4 Replies - 499 Views - Last Post: 07 February 2012 - 01:31 PM Rate Topic: -----

Topic Sponsor:

#1 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 29
  • View blog
  • Posts: 522
  • Joined: 25-November 10

subprocess.call doesn't change directory

Posted 07 February 2012 - 11:57 AM

Hi, I want to simulate the cd command as applicable in command prompt via subprocess.call. However it is is showing an error as shown in the figure? What is the reason of the error, and how might I achieve what I want to?

Attached image(s)

  • Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: subprocess.call doesn't change directory

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 443
  • View blog
  • Posts: 792
  • Joined: 08-June 10

Re: subprocess.call doesn't change directory

Posted 07 February 2012 - 12:04 PM

The error may be because you forgot to escape the "\". The proper way to point to a directory on Windows is with double-backslashes, or via os.path.join:
# While this works...
"cd C:\\Perl\\Codes"

# ...this is preferable because it is cross platform
import os
os.path.join(["c:", "Perl", "Codes"])



Also, subprocess won't work for changing directories; you'll find the call only works on the subprocess itself, not the whole script. What you really want is os.chdir().
Was This Post Helpful? 1
  • +
  • -

#3 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 29
  • View blog
  • Posts: 522
  • Joined: 25-November 10

Re: subprocess.call doesn't change directory

Posted 07 February 2012 - 12:18 PM

Trust me, escaping the backlashes was the first thing I tried after I got this error! Still I got the same error!
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 621
  • View blog
  • Posts: 1,038
  • Joined: 21-June 11

Re: subprocess.call doesn't change directory

Posted 07 February 2012 - 01:15 PM

It doesn't work because cd is a shell-builtin, not an actual executable and subprocess.call doesn't go through the shell by default. The error message is telling you that it can't find an executable named "cd".

If you pass Shell=True as an argument, you will no longer get an error, but it will still not do what you want for the same reason that you can't use subprocess.call to set environment variables: Executing commands that change the environment in a shell will only affect the environment of that shell, not of the parent process that spawned it.

To change the directory of your python script, you should use os.chdir.

PS: If you care about cross-platform-compatibility you should pass a list to subprocess.call when invoking an application with arguments. Using a string for that only works on Windows. On other OSs the whole string would be interpreted as the path to the executable.

This post has been edited by sepp2k: 07 February 2012 - 01:16 PM

Was This Post Helpful? 3
  • +
  • -

#5 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 29
  • View blog
  • Posts: 522
  • Joined: 25-November 10

Re: subprocess.call doesn't change directory

Posted 07 February 2012 - 01:31 PM

Wow, that was helpful! Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1