Receiving "SyntaxError: unexpected character after line continuation character" when trying to change directory

79 views Asked by At

Fair warning: I am completely new to python and coding in general. I recently purchased Eric Matthes's third edition "Python Crash Course". I have not even made it past the first chapter without encountering a problem. I am trying to run a file I have already written in a text editor inside my terminal by changing the directory to where this file is located, but it will not work no matter what I do. I have tried using online methods to change the directory and the book's exact way, but it doesn't work either way. If this matters, I am on Python 3.12 and using windows. I want to follow this book, so I am going to attempt to solve the errors I receive when using the book's method. The python file I want to load is in a specific python folder I made which is in my desktop along the file path. The file works perfectly fine in the text editor, but I cant get it to load in the terminal.

The code is supposed to look like this, according to the book:

C:\> cd Desktop\python_work
C:\Desktop\python_work> dir
hello_world.py
C:\Desktop\python_work> python hello_world.py
Hello Python World!

What happens to me:

C:\> cd Desktop\python_work
file "<stdin>", line 1
C:\> cd Desktop\python_work
   ^
SyntaxError: unexpected character after line continuation character  
1

There are 1 answers

0
iamdeedz On

Note: Anywhere I say "shell", "command processor" or "cmd", I am referring to the command prompt or powershell on Windows. Apologies for any confusion this may cause.

Problem

Your problem here is that you are attempting to run a windows command processor (cmd) command in a python shell. cd is the command to change the current working directory when in a windows shell. You were however in an interactive python shell meant for doing things like print("Hello World!") which is python code.

Solution

If you're on Windows 10, you can access the shell via the search bar in the bottom-left hand corner of your screen. Go there, type "cmd" and hit enter. Now you run your command (cd Desktop\python_work).

Alternatively, you can run your python file by navigating to the the folder in Windows File Explorer and running it through there. However if you want follow your book, it's probably best to use the shell to run your scripts as this also teaches you a bit about the shell and how it works/how to use it.

Hope this helps!