From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Hongyi Zhao Newsgroups: gmane.emacs.help Subject: Re: Set the default-directory dynamically according to the buffer-file-name. Date: Wed, 29 Sep 2021 22:50:44 +0800 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="36287"; mail-complaints-to="usenet@ciao.gmane.io" To: help-gnu-emacs Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Wed Sep 29 16:51:33 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mVavk-000989-Ox for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 29 Sep 2021 16:51:32 +0200 Original-Received: from localhost ([::1]:56604 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mVavi-0000Gx-Pn for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 29 Sep 2021 10:51:30 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:38764) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mVavD-0000Do-KS for help-gnu-emacs@gnu.org; Wed, 29 Sep 2021 10:51:00 -0400 Original-Received: from mail-vk1-xa2f.google.com ([2607:f8b0:4864:20::a2f]:36476) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mVavB-000584-9o for help-gnu-emacs@gnu.org; Wed, 29 Sep 2021 10:50:59 -0400 Original-Received: by mail-vk1-xa2f.google.com with SMTP id f126so612477vke.3 for ; Wed, 29 Sep 2021 07:50:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-transfer-encoding; bh=oTlU2nsAW6hmq5bRAOcE5opDyH4Fy2wygf4b4CZ0Q7k=; b=eyhQg/xOlGMxNlWEOkqY/WUmTdIDKFF2hhg3DEJG4uOkiuiaTYHoCvDzZZ0BBWK9Lz PuL76TMXdtDv74E3YMX1QaBOkc6eGte5Toz00JfjaCSIogNQOVjff2TuEjyRAzK3bFN/ TXGRwYbueSmCfiOIyAmUpUGkqdKw34/2d9qK7gVACgXXo7DSsZ6G0gcS0+n8RMP2tTAh VTdtz/xZPGnBOx5m2VvpWy/GRLWrD2rvTMpt7rJbY8DCfKjxhure7J4twjIVjaVDIi2V S0bnQZpSEXNRiOIw9FQYYg8MJlkAU+wS3b8KoUHWz0A7CnLRtgsvrWK3LRsUZ/xz3mMN tLOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:content-transfer-encoding; bh=oTlU2nsAW6hmq5bRAOcE5opDyH4Fy2wygf4b4CZ0Q7k=; b=bwCHI3vKKUEdL0yD1wJ7eMo52C1XatmahQKb8OSl5evylTr69t24yelMSZZftSFxUS vYIl4GSJkwuZWAjNQn9McaDSNL+NyGyQ1y6Vgz0eZZMJJCOJS3QHle5Eps4Man/UkVWH G4TnkycVWTqgj7EDsAcmd4PdJbAS+UFAM3e9/d9PTsTUdZjIbdIKCwoez4jgscvChs9I 8P4n0yPiSXPn67ziGofgAxMjUf7n+Arp2uGvMNR4VEPGRXVXZxqY2DpqeQ4N73jEw4Ss ag96vnbtI9gbxpKyHkrsn2byEV2+iYeTJ48SmBDRsV81eMfRPrr6e++RRF6hjAKdOabT Cocw== X-Gm-Message-State: AOAM530LbkNjCfyUNw/wxcFnwRW7bizLZ/Njeum9ZhM3MLy4FuY3asmJ 0eh+OW69rB6Twu1F0/msB3finE0SqpOMwKaWPpfhlaZhs8srCzaa X-Google-Smtp-Source: ABdhPJwq+TITZYuWk7P58bGVAFTxy0BVR82Wx9YI+0H9FuBtCRrtYILs1otOt4/C4Ct7c0eaIisCSEAzW55xOEPcR6A= X-Received: by 2002:a05:6122:2c3:: with SMTP id k3mr54311vki.6.1632927056117; Wed, 29 Sep 2021 07:50:56 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::a2f; envelope-from=hongyi.zhao@gmail.com; helo=mail-vk1-xa2f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:133394 Archived-At: On Wed, Sep 29, 2021 at 10:31 PM Hongyi Zhao wrote: > > I wrote the following function to set the default-directory > dynamically according to the buffer-file-name: > > (defun my/default-directory () > (let ((file (buffer-file-name))) > (if (not file) > (message "no file name here.") > (setq default-directory (file-name-directory (buffer-file-name)))= ))) > > Then I apply this function to specific hooks, say, python-mode-hook. > This will make it easier for me to invoke files using the relative > path of each project. I want to know if this is a good practice. Maybe I have some wrong understanding. Please let me explain the current situation carefully from the questions I describe below. I've python script which located at `/home/werner/Desktop/work/python', named as `pandas-excel.py', which will read a xls file located in the same folder as it. See following for the detailed content of this script: ####### import pandas as pd excel_file =3D '2021-2022-1-trimmed-top-3-lines.xls' newpd =3D pd.read_excel(excel_file, sheet_name=3D'Sheet1') for i in newpd.index: if i > 1: for j in newpd.columns: if int(j.split()[1]) > 2: if not pd.isnull(newpd.loc[i][j]): print(newpd.loc[i][j]) ####### Now I've added the above customized function to python-mode-hook. When I open the above script within Emacs and eval it with ipython, the following error will be triggered: /home/werner/2021-2022-1-trimmed-top-3-lines.xls Traceback (most recent call last): File "/home/werner/Desktop/work/python/pandas-excel.py", line 14, in newpd =3D pd.read_excel(excel_file, sheet_name=3D'Sheet1') File "/home/werner/.pyenv/versions/3.9.1/envs/datasci/lib/python3.9/site-= packages/pandas/util/_decorators.py", line 311, in wrapper return func(*args, **kwargs) File "/home/werner/.pyenv/versions/3.9.1/envs/datasci/lib/python3.9/site-= packages/pandas/io/excel/_base.py", line 364, in read_excel io =3D ExcelFile(io, storage_options=3Dstorage_options, engine=3Dengine= ) File "/home/werner/.pyenv/versions/3.9.1/envs/datasci/lib/python3.9/site-= packages/pandas/io/excel/_base.py", line 1191, in __init__ ext =3D inspect_excel_format( File "/home/werner/.pyenv/versions/3.9.1/envs/datasci/lib/python3.9/site-= packages/pandas/io/excel/_base.py", line 1070, in inspect_excel_format with get_handle( File "/home/werner/.pyenv/versions/3.9.1/envs/datasci/lib/python3.9/site-= packages/pandas/io/common.py", line 710, in get_handle handle =3D open(handle, ioargs.mode) FileNotFoundError: [Errno 2] No such file or directory: '2021-2022-1-trimmed-top-3-lines.xls' But `C-h o default-directory RET' will give me the following info: default-directory is a variable defined in =E2=80=98C source code=E2=80=99. Its value is "/home/werner/Desktop/work/python/" Local in buffer pandas-excel.py; global value is nil Therefore, I want to know why it does not locate the xls file from the directory of the Python script. Regards --=20 Assoc. Prof. Hongyi Zhao Theory and Simulation of Materials Hebei Vocational University of Technology and Engineering No. 473, Quannan West Street, Xindu District, Xingtai, Hebei province