help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Set the default-directory dynamically according to the buffer-file-n


From: Hongyi Zhao
Subject: Re: Set the default-directory dynamically according to the buffer-file-name.
Date: Wed, 29 Sep 2021 22:50:44 +0800

On Wed, Sep 29, 2021 at 10:31 PM Hongyi Zhao <hongyi.zhao@gmail.com> 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 = '2021-2022-1-trimmed-top-3-lines.xls'

newpd = pd.read_excel(excel_file, sheet_name='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 <module>
    newpd = pd.read_excel(excel_file, sheet_name='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 = ExcelFile(io, storage_options=storage_options, engine=engine)
  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 = 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 = 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 ‘C source code’.

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
-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
No. 473, Quannan West Street, Xindu District, Xingtai, Hebei province



reply via email to

[Prev in Thread] Current Thread [Next in Thread]