gpt4 book ai didi

python - subprocess.Popen 简单代码不允许我执行 cd(更改目录)

转载 作者:太空狗 更新时间:2023-10-29 20:20:09 34 4
gpt4 key购买 nike

我正在尝试使用 Python 脚本更改目录,但出现错误。

python 代码:

import subprocess
p = subprocess.Popen(['cd', '~'], stdout=subprocess.PIPE)
output = p.communicate()
print output

我收到这个错误:

File "test_sub.py", line 2, in <module>
p = subprocess.Popen(['cd', '~'], stdout=subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

错误是什么意思,我做错了什么,以及如何更改 python 子进程中的目录?

最佳答案

>>> Popen('cd ~', shell=True, stdout=PIPE).communicate()
(b'', None)

没有 shell=True (在默认为 /bin/shPOSIX 上,在 shell 中运行命令)

>>> Popen(['cd', '~'], stdout=PIPE).communicate()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/subprocess.py", line 858, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.4/subprocess.py", line 1456, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'cd'
>>>

除非您通过以下方式更改目录,否则您无法更改目录:

import os
os.chdir(os.path.abspath(os.path.expanduser('~')))

所以问题不是路径 ~ 不存在,而是 cd 不作为选项存在,除非你的命令在 shell 中运行支持它。直接传递给实际的 shell 使 cd 工作。但请注意 shell=True 是有风险的,除非你需要,否则永远不要使用它..
因此,请改用 os.chdir

一个工作场景:

import os, subprocess
os.chdir(os.path.abspath('/tmp/'))
print(subprocess.Popen(['ls', '-lah'], stdout=subprocess.PIPE).communicate()[0].decode('utf-8'))

导致:

[torxed@archie ~]$ python
Python 3.4.1 (default, May 19 2014, 17:23:49)

>>> import os, subprocess
>>> os.chdir(os.path.abspath('/tmp/'))
>>> print(subprocess.Popen(['ls', '-lah'], stdout=subprocess.PIPE).communicate()[0].decode('utf-8'))

total 12K
drwxrwxrwt 9 root root 220 Jun 11 12:08 .
drwxr-xr-x 19 root root 4.0K May 28 08:03 ..
drwxrwxrwt 2 root root 40 Jun 11 09:30 .font-unix
drwx------ 2 torxed users 60 Jun 11 09:33 gpg-LBLcdd
drwxrwxrwt 2 root root 40 Jun 11 09:30 .ICE-unix
drwx------ 2 torxed users 80 Jun 11 09:34 .org.chromium.Chromium.LEqfXB
-rw------- 1 torxed users 153 Jun 11 09:34 serverauth.EHWB0LqCv6
drwxrwxrwt 2 root root 40 Jun 11 09:30 .Test-unix
-r--r--r-- 1 root users 11 Jun 11 09:34 .X0-lock
drwxrwxrwt 2 root root 60 Jun 11 09:34 .X11-unix
drwxrwxrwt 2 root root 40 Jun 11 09:30 .XIM-unix

>>>

请注意,我在 ~ 中启动了 shell,并通过 os.chdir 将其更改为 tmp 并实际获得了我的 tmp 目录内容。

shell和命令的解释:

shell 命令是内置在 shell 中的东西,而常规的旧命令是您可以在 /bin 下找到的东西,例如:

[torxed@archie ~]$ ls /bin
2to3 2to3-2.7
7z 7za
...

其中 7z 是我实际可以执行的命令:

>>> from subprocess import *
>>> Popen(['7z'], stdout=PIPE).communicate()

(b'\n7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18\np7zip Version 9.20 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,8 CPUs)\n

虽然 cd 是一个内置的 shell 命令,但在 /bin 下找不到,但在大多数“终端”(使用 shell)中仍然可以使用因为它(如前所述)内置于您通常看到的外壳中。

但是由于默认情况下 Python 不会在 shell 中执行命令,您或多或少必须依赖于使用 os.chdir(...) 或将命令包装在 /bin/sh - c "cd ..." 或类似的东西。

关于python - subprocess.Popen 简单代码不允许我执行 cd(更改目录),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24161476/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com