unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Hongyi Zhao <hongyi.zhao@gmail.com>
To: help-gnu-emacs <help-gnu-emacs@gnu.org>
Subject: Re: Set the default-directory dynamically according to the buffer-file-name.
Date: Wed, 29 Sep 2021 22:50:44 +0800	[thread overview]
Message-ID: <CAGP6POLtRhA0tqCnpbDdh=cV2k+C0tSxVWpE=mmm1Gqf+dPTyQ@mail.gmail.com> (raw)
In-Reply-To: <CAGP6POLJ6qFegRByXLYAD78_aZA+Zg=FkT0OeFFoPBMP1c-jrw@mail.gmail.com>

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	other threads:[~2021-09-29 14:50 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-29 14:31 Set the default-directory dynamically according to the buffer-file-name Hongyi Zhao
2021-09-29 14:50 ` Hongyi Zhao [this message]
2021-09-29 15:31 ` Tassilo Horn
2021-09-30  1:08   ` Hongyi Zhao
2021-09-30  3:37     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-30  7:00       ` Hongyi Zhao
2021-09-30  7:39         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-30  8:54           ` Hongyi Zhao
2021-09-30  9:58             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-29 15:47 ` [External] : " Drew Adams
2021-10-01  4:41   ` Hongyi Zhao
2021-10-01  7:09     ` Emanuel Berg via Users list for the GNU Emacs text editor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAGP6POLtRhA0tqCnpbDdh=cV2k+C0tSxVWpE=mmm1Gqf+dPTyQ@mail.gmail.com' \
    --to=hongyi.zhao@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).