Python 命令行在不同文件夹中执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13744473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Command line execution in different folder
提问by Victor
I'm calling a command line program in python using the os.system(command)call.
我正在使用调用在 python 中调用命令行程序os.system(command)。
How can I call this command passing a different folder for execution? There is a system call for this? Or I should save the current folder, and, after execution, change restore it.
如何调用此命令传递不同的文件夹以执行?有系统调用吗?或者我应该保存当前文件夹,并在执行后更改恢复它。
采纳答案by hynekcer
The subprocessmodule is a very good solution.
该subprocess模块是一个非常好的解决方案。
import subprocess
p = subprocess.Popen([command, argument1,...], cwd=working_directory)
p.wait()
It has also arguments for modifying environment variables, redirecting input/output to the calling program, etc.
它还具有用于修改环境变量、将输入/输出重定向到调用程序等的参数。
回答by zenpoy
Try to os.chdir(path)before invoking the command.
os.chdir(path)在调用命令之前尝试。
From here:
从这里:
os.chdir(path) Change the current working directory to path.
Availability: Unix, Windows
os.chdir(path) 将当前工作目录更改为 path。
可用性:Unix、Windows
EDIT
编辑
This will change the current working dir, you can get the current working by:
这将更改当前工作目录,您可以通过以下方式获取当前工作:
os.getcwd()
If you want to save it and restore it later, if you need to do some work in the original working dir.
如果您想保存它并稍后恢复它,如果您需要在原始工作目录中做一些工作。
EDIT 2
编辑 2
In any case you should probably move to subprocess(doc) as suggested here. If you use subprocess's Popenyou have the choice of providing cwdparameter to specify the working directory for the subprocess: read this.
在任何情况下,您都应该按照此处的建议转到subprocess( doc) 。如果您使用's您可以选择提供参数来指定子进程的工作目录:阅读此内容。subprocessPopencwd
subprocess.Popen(args, bufsize=0, executable=None, stdin=None,
stdout=None, stderr=None, preexec_fn=None, close_fds=False,
shell=False, cwd=None, env=None, universal_newlines=False,
startupinfo=None, creationflags=0)
...
If cwd is not None, the child's current directory will be changed to cwd before it is executed. Note that this directory is not considered when searching the executable, so you can't specify the program's path relative to cwd.
...
如果 cwd 不是 None,则在执行之前,子项的当前目录将更改为 cwd。请注意,搜索可执行文件时不考虑此目录,因此您不能指定程序相对于 cwd 的路径。
回答by Pierre Olivier Downey
Here, I made a little function to change the path you're working on :
在这里,我做了一个小函数来改变你正在处理的路径:
import os
def make_path(r_path):
ack = 1
try:
root = os.path.dirname(__file__)
rel_path = os.path.join("..", r_path)
abs_path = os.path.join(root, rel_path)
os.chdir(abs_path)
ack = 0
except Exception as details:
print('problem to get to the path '+r_path+' (0001) : ' + str(details))
return ack
So here, r_pathis the relative path you want to go to. I added ".."to the path.join()methode so, if you're in a folder and want to exit it before searching for your path, it does it automatically. So, if your relative directory looks like this :
所以这里,r_path是你想要去的相对路径。我添加".."到path.join()方法中,因此,如果您在文件夹中并想在搜索路径之前退出它,它会自动执行。因此,如果您的相对目录如下所示:
-path_to_reach
-a_random_file.txt
-your_current_folder
-your_program.py
you can do those lines to go inside the path_to_reachand make your command, in exemple :
您可以执行这些行以进入path_to_reach并执行命令,例如:
command = ls
make_path('path_to_reach/')
os.system(command)
This command would not be useful, but you see the idea!
这个命令没有用,但你看到了这个想法!

