子过程更改目录
我想在子目录/superDirectory中执行一个脚本(我需要在此子/超级直接级别内部)。我无法获得子过程
输入我的子目录:
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 directory
Python抛出了Oserror,我不知道为什么。无论是尝试进入现有子介绍还是将一个目录升级到上述)都没关系 - 我总是遇到相同的错误。
I want to execute a script inside a subdirectory/superdirectory (I need to be inside this sub/super-directory first). I can't get subprocess
to enter my subdirectory:
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 directory
Python throws OSError and I don't know why. It doesn't matter whether I try to go into an existing subdir or go one directory up (as above) - I always end up with the same error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您的代码试图做的是调用名为
cd ..
的程序。您想要的是称为命令cd
。但是
CD
是外壳内部。因此,您只能将其称为,但是这样做是毫无意义的。,由于没有过程可以更改另一个过程的工作目录(至少在Unix型OS上,但在Windows上也是如此),此呼叫将使子壳立即更改其DIR并立即退出。
您想要使用
os.chdir()
或subprocess
name parametercwd
在执行子进程之前会更改工作目录。例如,要在根目录中执行
ls
,您可以做或简单地执行
What your code tries to do is call a program named
cd ..
. What you want is call a command namedcd
.But
cd
is a shell internal. So you can only call it asBut it is pointless to do so. As no process can change another process's working directory (again, at least on a UNIX-like OS, but as well on Windows), this call will have the subshell change its dir and exit immediately.
What you want can be achieved with
os.chdir()
or with thesubprocess
named parametercwd
which changes the working directory immediately before executing a subprocess.For example, to execute
ls
in the root directory, you either can door simply
要运行
your_command
作为其他目录中的子进程,请通过cwd
参数,为在 @wim的答案中提出建议:一个子过程无法更改其父母的工作目录(通常)。运行
cd ..
在使用子过程的子进程中,不会更改您的父python脚本的工作目录, @glglgl的答案中的代码示例是错误的。cd
是一个shell内置(不是单独的可执行文件),它只能在相同进程中更改目录。To run
your_command
as a subprocess in a different directory, passcwd
parameter, as suggested in @wim's answer:A child process can't change its parent's working directory (normally). Running
cd ..
in a child shell process using subprocess won't change your parent Python script's working directory i.e., the code example in @glglgl's answer is wrong.cd
is a shell builtin (not a separate executable), it can change the directory only in the same process.subprocess.call
和subprocess
模块中的其他方法具有cwd
参数。此参数确定要执行过程的工作目录。
因此,您可以做类似的事情:
查看文档
subprocess.call
and other methods in thesubprocess
module have acwd
parameter.This parameter determines the working directory where you want to execute your process.
So you can do something like this:
Check out docs subprocess.popen-constructor
您想使用可执行文件的绝对路径,并使用
cwd
popen
的夸尔格来设置工作目录。请参阅 docs 。You want to use an absolute path to the executable, and use the
cwd
kwarg ofPopen
to set the working directory. See the docs.我想这些天你会这样做:
I guess these days you would do:
基于此答案的另一个选项: https://stackoverflow.com/a/a/29269316/451710
这允许您允许您执行多个多重执行多个多次多次在同一过程中的命令(例如
cd
)。Another option based on this answer: https://stackoverflow.com/a/29269316/451710
This allows you to execute multiple commands (e.g
cd
) in the same process.只需使用
os.chdir
示例:
just use
os.chdir
Example:
如果您想具有CD功能(假设Shell = true),并且仍然希望根据Python脚本更改目录,则此代码将允许“ CD”命令工作。
If you want to have cd functionality (assuming shell=True) and still want to change the directory in terms of the Python script, this code will allow 'cd' commands to work.
如果您需要更改目录,请运行命令并获取STD输出:
If you need to change directory, run a command and get the std output as well: