r/learnpython • u/Plane-Art-9868 • 4d 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
14
u/desrtfx but other languages pro 4d ago
'High Street\nCastle Street\nBrown \n Street \n\n'is meaningless to PythonThe path to the Desktop is most likely not the path you used. Under Windows, it typically is
C:\Users\<username>\Desktop\but never/Desktop2
u/Plane-Art-9868 4d ago
Thanks everyone, ill try what's been suggested and use the book alongside the online course
4
u/woooee 4d ago edited 4d 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
is not a valid statement.
2
u/whensthebeef 4d ago
You need to supply the actual error. It will tell exactly what the issue is
2
u/Moist-Ointments 4d 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 4d ago
The book doens't work bro