Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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 (119k 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.

Related questions

+1 vote
1 answer
0 votes
5 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...