python:将脚本工作目录更改为脚本自己的目录
我每分钟从 crontab 运行一个 python shell:
* * * * * /home/udi/foo/bar.py
/home/udi/foo
有一些必要的子目录,例如 /home/udi/foo/log
和 /home /udi/foo/config
,/home/udi/foo/bar.py
引用。
问题是 crontab 从不同的工作目录运行脚本,因此尝试打开 ./log/bar.log 失败。
有没有一种好方法告诉脚本将工作目录更改为脚本自己的目录?我想要一个适用于任何脚本位置的解决方案,而不是明确告诉脚本它在哪里。
I run a python shell from crontab every minute:
* * * * * /home/udi/foo/bar.py
/home/udi/foo
has some necessary subdirectories, like /home/udi/foo/log
and /home/udi/foo/config
, which /home/udi/foo/bar.py
refers to.
The problem is that crontab
runs the script from a different working directory, so trying to open ./log/bar.log
fails.
Is there a nice way to tell the script to change the working directory to the script's own directory? I would fancy a solution that would work for any script location, rather than explicitly telling the script where it is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这会将您当前的工作目录更改为,以便打开相对路径可以工作:
但是,您询问如何更改为 Python 脚本所在的任何目录,即使您不知道在编写脚本时该目录是什么脚本。为此,您可以使用 os.path 函数:
该函数获取脚本的文件名,将其转换为绝对路径,然后提取该路径的目录,然后更改为该目录。
This will change your current working directory to so that opening relative paths will work:
However, you asked how to change into whatever directory your Python script is located, even if you don't know what directory that will be when you're writing your script. To do this, you can use the
os.path
functions:This takes the filename of your script, converts it to an absolute path, then extracts the directory of that path, then changes into that directory.
您可以使用
sys.path[0]
获得较短的版本。来自 http://docs.python.org/library/sys.html#sys。路径
You can get a shorter version by using
sys.path[0]
.From http://docs.python.org/library/sys.html#sys.path
不要这样做。
您的脚本和数据不应混杂到一个大目录中。将您的代码放在与数据分开的某个已知位置(
site-packages
或/var/opt/udi
或其他位置)。对代码使用良好的版本控制,以确保当前版本和以前的版本相互分离,以便您可以回退到以前的版本并测试未来的版本。底线:不要混合代码和数据。
数据是宝贵的。代码来来去去。
提供工作目录作为命令行参数值。您可以提供默认值作为环境变量。不要推断它(或猜测它),
将其设为必需的参数值并执行此操作。
不要根据软件的位置“假定”目录。从长远来看,效果不会很好。
Don't do this.
Your scripts and your data should not be mashed into one big directory. Put your code in some known location (
site-packages
or/var/opt/udi
or something) separate from your data. Use good version control on your code to be sure that you have current and previous versions separated from each other so you can fall back to previous versions and test future versions.Bottom line: Do not mingle code and data.
Data is precious. Code comes and goes.
Provide the working directory as a command-line argument value. You can provide a default as an environment variable. Don't deduce it (or guess at it)
Make it a required argument value and do this.
Do not "assume" a directory based on the location of your software. It will not work out well in the long run.
将 crontab 命令更改为
(...)
启动一个子 shell,您的 crond 将其作为单个命令执行。 <代码>|| exit 1 会导致您的 cronjob 在目录不可用时失败。尽管从长远来看,对于您的特定脚本而言,其他解决方案可能更优雅,但在您无法修改要执行的程序或命令的情况下,我的示例仍然有用。
Change your crontab command to
The
(...)
starts a sub-shell that your crond executes as a single command. The|| exit 1
causes your cronjob to fail in case that the directory is unavailable.Though the other solutions may be more elegant in the long run for your specific scripts, my example could still be useful in cases where you can't modify the program or command that you want to execute.
以防万一,您也可以使用
pathlib
或者在单行中:
Just in case, you could also use
pathlib
Or in single line: