r/learnpython • • 2d ago

Keep getting error while following the steps in a book

Hi, I'm learning from a book, and it's been going well up until now. I'm following the exact steps it's telling me to, but I keep getting an error. I like to copy the code in the book, test it, then modify the contents etc afterward so it sticks in my head. This code however isn't seeming to work.

file = open("/Desktop/List.txt")

file.read()

'High Street\nCastle Street\nBrown \n Street \n\n'

file.close()

I have tried making a text file on my desktop named List.txt, List, and I have tried inputting the street name into the document to see if that's where I was going wrong. I've been stuck on this for 2 days, so any help would be great. Thank you

5 Upvotes

19 comments sorted by

14

u/desrtfx but other languages pro 2d ago
  1. Exact error?

  2. Your code alone cannot work - the line 'High Street\nCastle Street\nBrown \n Street \n\n'is meaningless to Python

  3. What book, what chapter, what exercise?

The path to the Desktop is most likely not the path you used. Under Windows, it typically is C:\Users\<username>\Desktop\ but never /Desktop

0

u/Plane-Art-9868 2d ago

I'm using the 'Beginners step by step coding course' It is in the python section (there's html, scratch, and javascript in the book too) and it's the input and output bit

12

u/desrtfx but other languages pro 2d ago

Okay, there's a lot to unpack.

The book uses IDLE - the Python REPL environment and you typed the response into your code with the line I quoted before.

The only lines you should type are the ones with the prompt >>> at the start. The other lines are the responses. You must not type them.

The Path to Desktop can never work, as I said before.

Please, do yourself a favor and ditch the book. Use the MOOC Python Programming 2026 from the University of Helsinki. It is free, textual, extremely practice oriented, top quality, and a proper course.

1

u/Plane-Art-9868 2d ago

Thank you, I'll try the course you have provided

0

u/Baner-Fournadet 2d ago

technically a bare string literal is valid python so that line wont throw an error, it just looks like OP copied the REPL output along with the actual commands

2

u/Plane-Art-9868 2d ago

Thanks everyone, ill try what's been suggested and use the book alongside the online course

4

u/woooee 2d ago edited 2d ago

We can't solve an error when we don't know what it is. I will guess that it has something to do with "could not find the file". Try using the complete path and file name. Note that Python recognizes a forward slash (/) and converts it to whatever your OS uses. On all computers the backslash has a different meaning so get in the habit of using a forward slash

Next, you don't store the result of the read() so this can't be an exact copy from the book.

Next

'High Street\nCastle Street\nBrown \n Street \n\n'

is not a valid statement.

4

u/dkozinn 2d ago edited 2d ago

If OP is typing this into a REPL (python, ipython, etc.) that will produce the exact results, assuming that the file exists and contains those contents.

'High Street\nCastle Street\nBrown \n Street \n\n' is not a valid statement.

Actually it is valid, though not particularly useful. Pylint says:

W0105: String statement has no effect (pointless-string-statement)

Regardless, even if the file did exist, unless OP is using a REPLY (which will show the contents of the read(), it's not going to do anything.

0

u/Plane-Art-9868 2d ago

I just posted the error, but that really is how the book is telling me how to do it

3

u/danielroseman 2d ago

It really really isn't.

2

u/woooee 2d ago

See my post about using the entire path and file name. The computer used for the book's code is likely set up differently than your computer. Run this code to get the parent directories

from pathlib import Path
parent_dirs = Path(__file__).parent.parent
print(parent_dirs)

Then use these root directories plus the current directory your file is in, and the file name. Finally, don't spend 2 days trying to figure it out, ask someone who knows / post the question here.

2

u/whensthebeef 2d ago

You need to supply the actual error. It will tell exactly what the issue is

2

u/Plane-Art-9868 2d ago

Sorry, the error is

Traceback (most recent call last):

File "C:\Users\har\Desktop\python book\user inputs.py", line 9, in <module>

file = open("/Desktop/List.txt")

FileNotFoundError: [Errno 2] No such file or directory: '/Desktop/List.txt'

4

u/whensthebeef 2d ago

Last line tells you the problem. Either your file path doesn’t exist, or you structured it incorrectly in the string

2

u/codeguru42 1d ago

It sounds like you need to learn about a computer's filesystem. Especially focus on how to locate a file. This is called a "path" and works differently on windows, linux, and Mac. You might have a typo or the book's author is using Mac or Linux and you are on Windows. You will need to do two things:

  1. Double check that the input file is saved somewhere on your hard drive.

  2. Find the path to the file and edit your code with it.

2

u/Ok-Market4287 5h ago

Make in your python book folder a file called List.txt
Put in it what you want line for line
Chance the open to
file=open(“List.txt”)

3

u/dkozinn 2d ago

FileNotFoundError: [Errno 2] No such file or directory: '/Desktop/List.txt'

It's telling you that the file named List.txt is not found in a folder (or directory) called "/Desktop".

Looks like you're using Windows, so open Explorer and go to that directory to see if that file is there. If not, create it with something like Notepad and you should be good.

2

u/Moist-Ointments 2d ago

Just a PSA: Programming books (any book) can have errors. Especially earlier editions for new stuff. I have found them myself.

Most publishers are happy to accept corrections (at least in my experience) when you send them what you found (as long as you can present the case about why it's incorrect), but obviously the book can't be corrected until a new addition is printed. If the book publisher keeps an errata page online, you can look there for information on corrections while you're going through the book. See if your publisher has one for the book you are using, so if you run into stuff like this you can see if someone has reported it and a correction has been posted.

-2

u/EnvironmentalDot9131 2d ago

The book doens't work bro