subprocess.call doesn't change directory
Page 1 of 14 Replies - 499 Views - Last Post: 07 February 2012 - 01:31 PM
Topic Sponsor:
#1
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?
Replies To: subprocess.call doesn't change directory
#2
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:
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().
# 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().
#3
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!
#4
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.
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
#5
Re: subprocess.call doesn't change directory
Posted 07 February 2012 - 01:31 PM
Wow, that was helpful! Thanks!
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|