我想在子目錄/超級目錄中執(zhí)行腳本(我首先需要在此子目錄/超級目錄中)。我無法subprocess進(jìn)入子目錄:tducin@localhost:~/Projekty/tests/ve$ pythonPython 2.7.4 (default, Sep 26 2013, 03:20:26) [GCC 4.7.3] on linux2Type "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_exceptionOSError: [Errno 2] No such file or directoryPython拋出OSError,我不知道為什么。無論
3 回答

慕姐4208626
TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個贊
要your_command在不同目錄中作為子進(jìn)程運(yùn)行cwd,請按照@wim的答案中的建議傳遞參數(shù):
import subprocess
subprocess.check_call(['your_command', 'arg 1', 'arg 2'], cwd=working_dir)
子進(jìn)程無法更改其父級的工作目錄(通常是)。cd ..使用子進(jìn)程在子shell進(jìn)程中運(yùn)行不會更改父Python腳本的工作目錄,即@glglgl的答案中的代碼示例錯誤。cd是內(nèi)置的shell(不是單獨(dú)的可執(zhí)行文件),它只能在同一過程中更改目錄。
添加回答
舉報
0/150
提交
取消