+ 1

What's wrong with my code?

file = open("Test.txt") cont = file.read print (cont) file.close Why does this raise an SyntaxError in the first line??? Help please I am going crazy. Even if I write file = open("Test.txt", "r") it raises the same Error... Why?

27th Apr 2017, 7:34 AM
RedFoxX
46 ответов
+ 8
Yes, try to avoid naming variables like reserved keywords or builtins. In some cases it might confuse the interpreter. Idk if this is the case, but just for the __future__ ;)
27th Apr 2017, 11:12 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 8
😉
27th Apr 2017, 12:34 PM
Lazarus King
Lazarus King - avatar
+ 7
Bjarne Sohns I think u forgot a colon at the definiton of the function (for the code u posted in the comments)
27th Apr 2017, 10:32 AM
Lazarus King
Lazarus King - avatar
+ 7
ffllfl = open("Test.txt","w") ffllfl.write ("Hello World!") ffllfl.close() ffllfl=open("Test.txt","r") cont = ffllfl.read() print (cont) ffllfl.close()
27th Apr 2017, 11:40 AM
Lazarus King
Lazarus King - avatar
+ 7
working now?
27th Apr 2017, 11:42 AM
Lazarus King
Lazarus King - avatar
+ 7
are u trying it on sololearn
27th Apr 2017, 11:44 AM
Lazarus King
Lazarus King - avatar
+ 7
or maybe it has something to do with your version of python? are you using python 3
27th Apr 2017, 11:44 AM
Lazarus King
Lazarus King - avatar
+ 7
maybe for PC u need the files location on the pc, I don't know but try creating a .txt file using notepad store it somewhere and copy the location from the address bar it will probably be like: ffllfl=open("c://........"r") cont = ffllfl.read() print (cont) ffllfl.close() try that if it also doesn't work then @Kuba you're up next sir
27th Apr 2017, 12:15 PM
Lazarus King
Lazarus King - avatar
+ 7
sorry for not being able to help
27th Apr 2017, 12:28 PM
Lazarus King
Lazarus King - avatar
+ 6
You have to add a colon at the end of the first line and an ending parentheses in the last one.
27th Apr 2017, 8:53 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 6
Why don't you open the file in write mode, write sth to it then read it?
27th Apr 2017, 11:14 AM
Lazarus King
Lazarus King - avatar
27th Apr 2017, 11:30 AM
Lazarus King
Lazarus King - avatar
+ 6
on the fifth line make it ffllfl=open("Test.text",'r') instead of ffllfl.open. It'll work after that.
27th Apr 2017, 11:36 AM
Lazarus King
Lazarus King - avatar
+ 5
I ran this in Sololearn, changing the open() argument to __file__, so it wouldn't break down. All goes well if you add parentheses.
27th Apr 2017, 8:14 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
@Bjarne have you tried copying my code and running it? file = open(__file__) #there are double floors there cont = file.read() print (cont) file.close()
27th Apr 2017, 10:59 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
Then I don't know... Maybe it's the variable name which is also a builtin? Try changing it to f
27th Apr 2017, 11:07 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 4
cont = file.read() file.close() Have a nice day 😊
27th Apr 2017, 7:50 AM
Oren Cohen
Oren Cohen - avatar
+ 4
No, I added a self-reference to the code: file = open(__file__) cont = file.read() print (cont) file.close()
27th Apr 2017, 8:19 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 4
Go ahead, we are all here to learn and help each other. Try saving it to a code on SL though. It is much easier to comment on the code you see and can run instantly.
27th Apr 2017, 8:24 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar