命令行在不同文件夹中执行

时间:2021-01-09 15:52:41

I'm calling a command line program in python using the os.system(command) call.

我正在使用os.system(command)调用在python中调用命令行程序。

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.

如何调用此命令传递不同的文件夹以执行?系统调用了吗?或者我应该保存当前文件夹,并在执行后更改还原它。

2 个解决方案

#1


60  

The subprocess module is a very good solution.

子进程模块是一个非常好的解决方案。

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.

它还有修改环境变量,将输入/输出重定向到调用程序等的参数。

#2


6  

Try to os.chdir(path) before invoking the command.

在调用命令之前尝试os.chdir(path)。

From here:

从这里:

os.chdir(path) Change the current working directory to path.

os.chdir(path)将当前工作目录更改为path。

Availability: Unix, Windows

可用性: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 Popen you have the choice of providing cwd parameter to specify the working directory for the subprocess: read this.

在任何情况下,您都应该按照此处的建议移至子流程(doc)。如果使用子进程的Popen,则可以选择提供cwd参数来指定子进程的工作目录:读取此内容。

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的路径。

#1


60  

The subprocess module is a very good solution.

子进程模块是一个非常好的解决方案。

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.

它还有修改环境变量,将输入/输出重定向到调用程序等的参数。

#2


6  

Try to os.chdir(path) before invoking the command.

在调用命令之前尝试os.chdir(path)。

From here:

从这里:

os.chdir(path) Change the current working directory to path.

os.chdir(path)将当前工作目录更改为path。

Availability: Unix, Windows

可用性: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 Popen you have the choice of providing cwd parameter to specify the working directory for the subprocess: read this.

在任何情况下,您都应该按照此处的建议移至子流程(doc)。如果使用子进程的Popen,则可以选择提供cwd参数来指定子进程的工作目录:读取此内容。

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的路径。