0

I'm trying to use the cd command in order to point to the folder where my file is located but I keep getting SyntaxError: invalid syntax (see screenshot).

Could anyone please point out what I'm doing wrong here?

enter image description here

0

3 Answers 3

0

To complement @phoibos's answer, you are trying to change to a directory on another drive, you need to use the /d switch when in the normal cmd console.

cd /d D:\folder
0

First, you're using cd in a Python environment, which is not available here. You should use chdir from os module to change the current working path in Python.

Also, you use backslashes for defining the path, which need to be escaped. Either use D:\\back_this_up\\..... or D:/back_this_up/.......

Example:

import os
os.chdir("D:/back_this_up/your/desired/path")

and then do whatever you want to do.

If you want to use cd (as you described in your question) you have to exit the Python interpreter with exit() command and then use the syntax you tried before.

0

You're entering the cd command within an instance of the python interactive interpreter (notice the >>> at the beginning of the lines), not within the windows command prompt.

Try exiting python with CTRL+Z, then Enter. After this, enter your cd command.

Edit:

Maybe you just want to run a python program? Then you would would have to run

python path\to\your\pythonfile.py
2
  • if I'd like to run multiple files from the same folder, then do I need to specify the full path each time? My cd command was an attempt to avoid that. Thanks for your help!
    – RubenGeert
    Commented Feb 4, 2013 at 17:50
  • Yes, in the windows command prompt, change to the folder containing the python files with cd, the you can omit the full path; the filename is sufficient then: python srcfile.py.
    – phoibos
    Commented Feb 4, 2013 at 20:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .