0 votes
1 view
in Python by (20.3k points)

When I run the following script in IDLE

import os

print(os.getcwd())

I get the output as

D:\testtool

but when I run from cmd prompt, I get

c:\Python33>python D:\testtool\current_dir.py

c:\Python33

How do I get the same result which I got using IDLE?

1 Answer

0 votes
by (88.9k points)

This can be because of IDLE of python changing its CWD (current working directory) to the location where the python script is running. So, using CWD while running the script does not do anything and leaves the CWD  as same.

You can the following python code to change the CWD to the location where the script is running:

import os

os.chdir(os.path.dirname(__file__))

print(os.getcwd())

If you want to be a pro in Python then I recommend this Python Certification program by Intellipaat.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...