We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Lance Culnane • 5 years ago

In the 6th box, file = open(“testfile.text”, “r”) should be replaced with file = open(“testfile.txt”, “r”)

vinayak basavaraddi • 5 years ago

why ?? I am not seeing the difference between two.

Luiz Polli • 5 years ago

The difference is the word "text" and "txt".

Kristy Whalen • 4 years ago

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

nani • 4 years ago

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+

Max Unch • 5 years ago

You need append ".txt" at the end of the file name:

F = open("workfile.txt", 'w')

That's work fine!

vinayak basavaraddi • 5 years ago

Only possibility of failing of this command is syntax check. Python never minds about usage of lowercase or uppercase unless you are okay.

Amir Yunas • 4 years ago

Amazing tutorial on IO with python. learned alot. thanks!

Robert Nix • 4 years ago

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

Luiz Polli • 5 years ago

How can we count the lines in a file for example?

sick guy • 5 years ago

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

anthony_moore123 • 5 years ago

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.

Andrew Gatlin • 5 years ago

python requires / vs \ in the path

sabatha • 5 years ago

Hi...i get an error when I'm opening a fine.Any help please

chandan • 5 years ago

could you share your code...