cd目录不存在?

所以,我做了一个叫做jel的命令,它可以像jel那样jel 。 它运行在Python,当我运行jel doctor ,在jel.py它给了我一个错误(主文件)。 代码如下所示: 请注意,所有必需的模块已经被导入。

 elif arg == 'doctor': subprocess.call(['cd', 'js']) ver = subprocess.call(['node', 'version.js']) subprocess.call(['cd', '..']) if not ver == version: print 'jel doctor: \033[91found that version\033[0m ' + str(version) + ' \033[91mis not the current version\033[0m' print 'jel doctor: \033[92mrun jel update\033[0m' sys.exit() 

js文件version.js在节点上运行,如下所示: 安装所有必需的软件包

 var latest = require('latest'); latest('jel', function(err, v) { console.log(v); // => "0.0.3" if (err) { console.log('An error occurred.'); } }); 

jel.py文件使用subprocess jel.py调用cs jsnode version.js时,它给了我这个错误:

 Traceback (most recent call last): File "/bin/jel", line 90, in <module> subprocess.call(['cd', 'js']) File "/usr/lib/python2.7/subprocess.py", line 522, in call return Popen(*popenargs, **kwargs).wait() 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 bjskistad:~/workspace (master) $ jel doctor Traceback (most recent call last): File "/bin/jel", line 90, in <module> subprocess.call(['cd', 'js']) File "/usr/lib/python2.7/subprocess.py", line 522, in call return Popen(*popenargs, **kwargs).wait() 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 

我相信这是说目录不存在,虽然它。 我需要打电话给别的吗?

你的代码片段至less有三个问题:

  • cd是一个内置的shell,而不是一个可执行程序。 如果你想调用cd ,你需要调用shell。

  • cd命令只影响它运行的shell。 它不会影响python程序或任何后续的subprocess。

  • subprocess.call()的返回码不是程序写入stdout的文本。 要获取该文本,请尝试subprocess.check_output()

尝试这个:

 #UNTESTED elif arg == 'doctor': ver = subprocess.check_output(['cd js && node version.js'], shell=True) if not ver == version: 

正如已经指出的那样,改变目录只是反映在子过程中。 你应该使用os.chdir来改变你的工作目录,但是另外一个select就是把cwd指定到子进程中 ,这样可以避免使用cd或者os.chdir

  version = subprocess.check_output(['node', 'version.js'], cwd="js") 

你也应该使用!=在你的if和你可能想要rstrip的换行符:

  if version != ver.rstrip():