我想看看在python中确定当前脚本目录的最佳方法是什么?
我发现,由于调用python代码的方法很多,很难找到一个好的解决方案.
这是一些问题:
__file__如果脚本执行时未定义exec,execfile__module__ 仅在模块中定义用例:
./myfile.pypython myfile.py./somedir/myfile.pypython somedir/myfile.pyexecfile('myfile.py') (来自另一个脚本,可以位于另一个目录中,并且可以有另一个当前目录.我知道没有完美的解决方案,但我正在寻找解决大多数情况的最佳方法.
最常用的方法是os.path.dirname(os.path.abspath(__file__)),如果你从另一个脚本执行脚本,这实际上不起作用exec().
任何使用当前目录的解决方案都将失败,根据调用脚本的方式或者可以在运行的脚本中更改它,可能会有所不同.
我想在子目录/超级目录中执行一个脚本(我需要先在这个子/超级目录中).我无法subprocess进入我的子目录:
tducin@localhost:~/Projekty/tests/ve$ python
Python 2.7.4 (default, Sep 26 2013, 03:20:26)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> import os
>>> os.getcwd()
'/home/tducin/Projekty/tests/ve'
>>> subprocess.call(['cd ..'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1308, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or …Run Code Online (Sandbox Code Playgroud) 我正在使用crontab为我的minecraft服务器运行维护脚本.大多数情况下它工作正常,除非crontab尝试使用重启脚本.如果我手动运行重启脚本,则没有任何问题.因为我认为它与路径名称有关,所以我试图确保它总是从Minecraft目录中执行任何minecraft命令.所以我在pushd/popd中包含命令:
os.system("pushd /directory/path/here")
os.system("command to sent to minecraft")
os.system("popd")
Run Code Online (Sandbox Code Playgroud)
下面是一个互动会话,将我的世界排除在外.一个简单的"ls"测试.正如你所看到的,它根本没有运行来自pushd目录的os.system命令,而是来自/ etc /,这是我运行python的目录,以说明我的观点.Clearly pushd无法通过python运行所以我想知道我还能做到这一点.谢谢!
>>> def test():
... import os
... os.system("pushd /home/[path_goes_here]/minecraft")
... os.system("ls")
... os.system("popd")
...
>>> test()
~/minecraft /etc
DIR_COLORS cron.weekly gcrypt inputrc localtime mime.types ntp ppp rc3.d sasldb2 smrsh vsftpd.ftpusers
DIR_COLORS.xterm crontab gpm-root.conf iproute2 login.defs mke2fs.conf ntp.conf printcap rc4.d screenrc snmp vsftpd.tpsave
X11 csh.cshrc group issue logrotate.conf modprobe.d odbc.ini profile rc5.d scsi_id.config squirrelmail vz
adjtime csh.login group- issue.net logrotate.d motd odbcinst.ini profile.d rc6.d securetty ssh warnquota.conf
aliases …Run Code Online (Sandbox Code Playgroud) 使用Python pathlib (文档)功能更改目录的预期方法是什么?
让我们假设我创建一个Path对象如下:
from pathlib import Path
path = Path('/etc')
Run Code Online (Sandbox Code Playgroud)
目前我只知道以下内容,但这似乎破坏了这个想法pathlib.
import os
os.chdir(str(path))
Run Code Online (Sandbox Code Playgroud) 我有一个在此目录下的python脚本:
work/project/test/a.py
Run Code Online (Sandbox Code Playgroud)
在里面a.py,我用来subprocess.POPEN从另一个目录启动进程,
work/to_launch/file1.pl, file2.py, file3.py, ...
Run Code Online (Sandbox Code Playgroud)
Python代码:
subprocess.POPEN("usr/bin/perl ../to_launch/file1.pl")
Run Code Online (Sandbox Code Playgroud)
在work/project /下,我键入以下内容
[user@machine project]python test/a.py,
Run Code Online (Sandbox Code Playgroud)
错误"file2.py,'没有这样的文件或目录'"
如何添加work/to_launch/,以便file2.py可以找到这些相关文件?
我试图通过python子进程运行git命令.我这样做是通过调用github的cmd目录中的git.exe来实现的.
我设法让大多数命令工作(init,remote,status)但是在调用git add时出错了.到目前为止这是我的代码:
import subprocess
gitPath = 'C:/path/to/git/cmd.exe'
repoPath = 'C:/path/to/my/repo'
repoUrl = 'https://www.github.com/login/repo';
#list to set directory and working tree
dirList = ['--git-dir='+repoPath+'/.git','--work-tree='+repoPath]
#init gitt
subprocess.call([gitPath] + ['init',repoPath]
#add remote
subprocess.call([gitPath] + dirList + ['remote','add','origin',repoUrl])
#Check status, returns files to be commited etc, so a working repo exists there
subprocess.call([gitPath] + dirList + ['status'])
#Adds all files in folder (this returns the error)
subprocess.call([gitPath] + dirList + ['add','.']
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
fatal: Not a git repository (or any of the parent …Run Code Online (Sandbox Code Playgroud) 我有一个可执行文件,可以从相机中提取图像并将其保存到当前目录。
我有一个调用此可执行文件的 python 应用程序。它位于不同的目录中。
我想要做的是让 python 程序调用可执行文件,并使可执行文件在它所在的当前目录中运行,而不是在运行 python 程序的同一目录中运行。
这可能吗?
python ×7
subprocess ×3
centos ×1
cmd ×1
cron ×1
dirname ×1
git ×1
github ×1
pathlib ×1
pythonpath ×1