简体   繁体   中英

cd directory doesn't exist?

So, I made a command called jel , which is executable as jel . It is run in Python, and when I run jel doctor , in jel.py it gives me a error(the main file). The code looks like this: Note that all necessary modules are already imported.

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()

The js file version.js is run on node, and looks like this: All necessary packages are installed

var latest = require('latest');

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

It is giving me this error when the jel.py file uses subprocess to call cs js and node 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

I believe it is saying the directory doesn't exist, although it does. Do I need to call something else before?

There are at least three problems with your code snippet:

  • cd is a shell built-in, not an executable program. If you want to invoke cd , you'll need to invoke the shell.

  • The cd command only affects the shell in which it runs. It will have no effect upon the python program, or any subsequent subprocesses.

  • The return code from subprocess.call() is not the text that the program wrote to stdout. To get that text, try subprocess.check_output() .

Try this:

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

As already pointed out changing the directory is only reflected in the subprocess. You should use os.chdir to change your working directory but another alternative is to specify the cwd to subprocess which avoids any need to cd or os.chdir :

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

You should also use != in your if and you probably want to rstrip the newline:

 if  version != ver.rstrip(): 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM