We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.
why ?? I am not seeing the difference between two.
The difference is the word "text" and "txt".
Clever. Normally I catch *everything* when it comes to comparing data. Not this time :D
F = open(“workfile”,”w”)
Print f
This would give error since the variable f is not defined (F is). Python minds usage of caps and lower letters, my friend
Here's a fixed version of your code:
f = open(“workfile.txt”,”r”)
print f
Remember to add the file extension and you opened the file in write mode and not read mode.
If you want to print the contents of the file, replace "print f" with "print f.read()"
Hope this helped!
Oh and one more thing. If you need to read AND write at the same time, use r+
You need append ".txt" at the end of the file name:
F = open("workfile.txt", 'w')
That's work fine!
Only possibility of failing of this command is syntax check. Python never minds about usage of lowercase or uppercase unless you are okay.
Amazing tutorial on IO with python. learned alot. thanks!
The code:
file.write(“Hello World”)
file.write(“This is our new text file”)
file.write(“and this is another line.”)
file.write(“Why? Because we can.”)
In fact writes one single long line. You need to add "\n" to the ends of the strings to have them output as different lines.
solved
solved
How can we count the lines in a file for example?
try putting this is a textfile [['C', 1], ['F', 4], ['A', 3], ['E', 5], ['A', 3], ['E', 5], ['B', 5], ['C', 1], ['F', 3], ['E', 4], ['C', 1], ['E', 2], ['A', 3], ['C', 4], ['F', 3]]
then try reading it and splitting it by each little section ['C', 1] for example.
PLease help me
How do you get and write files for the case where the data is not in the same directory as Python? - I've tried adding a path to the open command (e.g. open('c:\directory_name\file_name') but it returns syntax errors.
python requires / vs \ in the path
Hi...i get an error when I'm opening a fine.Any help please
could you share your code...
In the 6th box, file = open(“testfile.text”, “r”) should be replaced with file = open(“testfile.txt”, “r”)