16

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'
>>> 
Tunafish Ha
  • 189
  • 1
  • 1
  • 7
  • 1
    Maybe it is wrong kind of slash (`/ instead of \\ `) that causing it? – andrybak Dec 27 '13 at 07:04
  • Folders on the "Desktop" are not in the root directory, so that relative path will not work. Try e.g. `"\\Users\\\\Desktop\\the path"` (or similar, don't remember the exact path). – Some programmer dude Dec 27 '13 at 07:05
  • can you try as @JoachimPileborg said, i think that will work. – James Sapam Dec 27 '13 at 07:16
  • In your own words, if the current working directory is `C:\\Python32` and you want to find a folder named `chapter 3` that is in a folder named `headfirstpython`, that is on your desktop, **why do you think** that `'../headfirstpython/chapter 3'` is the relative path to that folder? **What do you think** is the path of the desktop? (Hint: it is **not** `C:\\`.) – Karl Knechtel Mar 15 '23 at 05:09

1 Answers1

9

I think a few things may be helpful.

It looks like you're on a windows system, so you should use double back slashes '\\' to separate the folders.

Second, if you're trying to change to a folder within the current folder, you should use a single dot, and not two, e.g. os.chdir('.\\folder')

Finally, if the folder you are trying to access is not a direct subfolder of the current working directory (or otherwise in your path), you need to include the full path to access it. Since you said it's on your desktop, you'd probably want something that looks like this:

import os
os.chdir('C:\\Users\\username\\Desktop\\headfirstpython') ## Where username is replaced with your actual username

From here, you could also change directories to the chapter3 subdirectory with the following

os.chdir('chapter3') 

Which is equivalent in this case with

os.chdir('.\\chapter3')

or, if you want to be wordy:

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

Hopefully that helps?

Brandon
  • 1,722
  • 1
  • 19
  • 32
  • In the book it just tells me to create a folder but not where so I just made a folder on the desktop. But this does clear things up. – Tunafish Ha Dec 27 '13 at 07:58
  • "It looks like you're on a windows system, so you should use double back slashes '\\' to separate the folders." **This is bad advice** - please see [How should I write a Windows path in a Python string literal?](/q/2953834). – Karl Knechtel Mar 15 '23 at 05:11