Python Os.system() Call Runs In Incorrect Directory
Solution 1:
you could also try something like this
os.chdir("C:\\to\\my\\folder")
printos.system("echo %CD%")
raw_input()
also to get the current working directory i use a different approach
cur_dir = os.path.abspath(".")
Solution 2:
os.getcwd()
isn't guarenteed to get the location of your script when it is called. Your coworker might be calling the script a different way or his computer (for some reason) handles the current working directory differently.
To get the actual script location you should use the following:
import osos.path.dirname(os.path.realpath(__file__))
As an example I wrote getcwd
and the above line in the same script and ran it from C:\
.
Results:
C:\>python C:\Users\pies\Desktop\test.pyC:\Users\pies\DesktopC:\
It depends on what your real purpose for this script is, whether you actually need the current working directory, or just the current scripts directory. As a little caveat this call will return a different directory if you call a script from script which then uses this call.
Post a Comment for "Python Os.system() Call Runs In Incorrect Directory"