Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

I made a folder on my desktop with the name "headfirstpython" and I need to change my current working directory to that folder and to the sub folder inside of it. I used os.getcwd() to get the current folder and it gives me 'C\Python32'. I used os.chdir('../headfirstpython/chapter3') to change the directory but its telling it cannot find the path

>>> import os 

>>> os.getcwd() 

'C:\\Python32' 

>>> os.chdir('../headfirstpython/chapter 3') 

Traceback (most recent call last): 

File "<pyshell#2>", line 1, in <module> os.chdir('../headfirstpython/chapter 3') 

WindowsError: [Error 3] The system cannot find the path specified: '../headfirstpython/chapter 3' 

>>> os.chdir('../headfirstpython/chapter3') 

Traceback (most recent call last): 

File "<pyshell#3>", line 1, in <module> 

os.chdir('../headfirstpython/chapter3') 

WindowsError: [Error 3] The system cannot find the path specified: '../headfirstpython/chapter3' 

>>>

1 Answer

0 votes
by (106k points)

To change the current working directory in Python if you are using windows you should use double backslashes '\\' to separate the folders.

You can use the below-mentioned code:-

import os 

os.chdir('C:\\Users\\username\\Desktop\\headfirstpython')

Related questions

Browse Categories

...