How do I fix this? The path is correct. I’m new

cd Desktop/Files/Chap01
File “”, line 1
cd Desktop/Files/Chap01
SyntaxError: invalid syntax. Perhaps you forgot a comma?

That is not Python. The cd command is used in most shells, but not Python.

Searching for “python cd” found the following answers:
On Stackoverflow: How to change the current directory.
On a site I did not know: How do I “cd” in Python?

Unfortunately it also showed some questionable answers. To do a reputable tutorial may be best: Beginner tutorial on There is also a good tutorial for experienced programmers on

import os
os.system("cd C:\Users\<UserName>\Desktop\files\...")


This does not work.

>>> import os
>>> os.getcwd()
>>> os.system(r'cd c:\windows')
>>> os.getcwd()

Hi S,

I’m a OS user, I was following instructions from someone also using OS. Is the code you suggested supposed to fix that error?

Hi SuperLiJunyi,

Using os.system launches a new process which executes the cd command in
the shell, but that only affects the new process, not the calling
process (Python).

You need to use os.chdir(path).

Also, beware of backslashes. Backslashes are special in Python strings.
Backslashes can have a special meaning, for example

\n  --> newline, not backslash n
\r  --> carriage return, not backslash r
\t  --> tab, not backslash t
\f  --> formfeed, not backslash f

so it is easy to get strings wrong with backslashes. For pathnames on
Windows, the simplest solution is to just use forward slashes instead:

path = "C:/Users/<UserName>/Desktop/files/"
1 Like

Hi Pete! Welcome to programming!

Do you mean you are an Apple Mac OS X user? Because we’re all OS users,

whether our OS (operating system) is Windows, Unix, Linux, BSD, or Mac

OS X. All of these OSes have slight differences.

The command you gave earlier will work in the operating system’s shell:

cd path/to/my/folder

but it doesn’t work in the Python interpreter. There, you need something

like this:

import os


You can tell the two environments apart:

  • If the prompt looks like $ or % or # then you are in the

operating system’s shell.

  • If the prompt looks like >>> then you are in the Python interactive

interpreter, sometimes also called a REPL (Read-Eval-Print Loop).

  • If the prompt looks like In [1]: (where the number gets bigger each

time you give a command) then you are in a special enhanced Python shell

such as IPython or Jupyter. The rules of what works there can be

complicated and I shall not talk about them here.

Probably the instructions you were given were to open the Terminal or

Console application, and then enter

cd path/to/my/files/

at the dollar sign or percentage sign prompt, then enter


(or maybe python3) to start the Python interactive interpreter. That

will print a welcome message and change the prompt to >>>. Once you

get to that point, it is too late to use the cd ... command any

longer, you have to use the Python spelling which I gave earlier.

Hi Steven,
Thank you so much for your time and that detailed explanation. I really appreciate it.

Thank you ,I forgot to add “r” before string :joy:
Add a few :

\ a
\ b
\ 0
\ x