[英]Python: Changing terminal current directory and calling script
Once i check a particular file is there in a folder, i want to call bash script titled nii-sdcme
from python. 一旦我检查了文件夹中是否有特定文件,
nii-sdcme
从python调用名为nii-sdcme
bash脚本。 But before calling this script in terminal, i want to cd to particular directory. 但是在终端中调用此脚本之前,我想将CD转到特定目录。 Can this be done in python?
可以在python中完成吗?
So steps to run this script in terminal looks like this: 因此,在终端中运行此脚本的步骤如下所示:
cd DICOM/
nii_sdcme N
Where N is some folderNumber. 其中N是某个folderNumber。 eg: 92
例如:92
cd DICOM/
nii_sdcme 92
Can some one please direct me how this can be implemented in python script? 有人可以指导我如何在python脚本中实现吗?
Many Thanks! 非常感谢!
The fastest way would be using os.chdir : 最快的方法是使用os.chdir :
import os
if __name__ == "__main__":
print "Current dir: %s" % os.getcwd()
os.chdir('/tmp/')
print "Current dir: %s" % os.getcwd()
Which outputs when called: 调用时输出:
Current dir: /home/borrajax/Documents/Tests/StackOverflow
Current dir: /tmp
Now, you mentioned that you want to call a particular script ( nii-sdcme
) within your script. 现在,您提到要在脚本中调用特定脚本(
nii-sdcme
)。 You are probably going to use subprocess.Popen to do that. 您可能要使用subprocess.Popen来做到这一点。 With the tools provided by the subprocess module, you can specify a
cwd
argument so the script (executable, rather) called "sees" that cwd
path as its run directory. 使用subprocess模块提供的工具,您可以指定
cwd
参数,以便名为“ sees”的脚本(可执行文件) 将 cwd
路径作为其运行目录。 Be aware that this sets the directory indicated in cwd
after the executable is called... what I mean by this is that Popen
needs to find the executable's path before setting the executable's run directory. 请注意,这将在调用可执行文件后设置
cwd
指示的目录...我的意思是, Popen
需要在设置可执行文件的运行目录之前找到可执行文件的路径。 Let's say you're in your /home/
and the nii-sdcme
script is located in /tmp/
. 假设您位于
/home/
, nii-sdcme
脚本位于/tmp/
。
This: 这个:
subprocess.Popen(['nii-sdcme'], cwd='/tmp/')
will fail, because the executable is not in the directories defined in the $PATH
environment variable. 将失败,因为该可执行文件不在
$PATH
环境变量中定义的目录中。 On the other hand, this: 另一方面,这是:
subprocess.Popen(['/tmp/nii-sdcme'], cwd='/tmp/')
will succeed. 将会成功。
From the subrprocess.Popen docs: 从subrprocess.Popen文档:
If cwd is not None, the child's current directory will be changed to cwd before it is executed.
如果cwd不为None,则子级的当前目录将在执行前更改为cwd。 Note that this directory is not considered when searching the executable, so you can't specify the program's path relative to cwd.
请注意,搜索可执行文件时不会考虑此目录,因此您无法指定程序相对于cwd的路径。
EDIT (As per OP's comment to this question) 编辑 (根据OP对这个问题的评论)
how about if i change os.chdir(desired/path) and then cann subprocess.call('nii_sdcme %s' %a)
如果我更改os.chdir(desired / path)然后cann subprocess.call('nii_sdcme%s'%a)怎么样
That'll make nii_sdcme
use desired/path
as run directory as well. 这也将使
nii_sdcme
使用desired/path
作为运行目录。 os.chdir
changes the path of the current process (your current interpreter). os.chdir
更改当前进程(当前解释器)的路径。 If you subsequently call your nii_sdcme
executable without specifying a cwd
argument, the spawned subprocess will use as current directory the current directory of the parent process. 如果您随后在未指定
cwd
参数的情况下调用nii_sdcme
可执行文件,则生成的子进程将使用父进程的当前目录作为当前目录。
(!) Careful: Even if you change the current directory of your executable through os.chdir
, you still need to provide the full path to your nii_sdcme
executable (unless nii_sdcme
is in one of the directories specified in the $PATH
environment variable) (!)注意:即使通过
os.chdir
更改了可执行文件的当前目录,您仍然需要提供nii_sdcme
可执行文件的完整路径(除非nii_sdcme
位于$PATH
环境变量中指定的目录之一中)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.