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

Atah • 9 years ago

fh = open("hello.txt","r")

I tried just simply putting this 1 line of code in.. but it an error message says: Unresolved reference 'open'

Can anyone help me?

iqbalhabibie habibie • 8 years ago

Yes the file hello.txt must be exist.Otherwise error or Unresolved reference 'open'. In other words if the file doesnt exist you must make file using :

fh=open("hello.txt","w")
then

Shani Pratap Singh • 6 years ago

Hi Atah,

You should give path of file where file does exist.

Eg. fh=open("c://documents/hello.txt","r")

Raviraj Gardi • 5 years ago

by doing fh=open("hello.txt",r) you are openin a file named 'hello.txt' and reading it {as r stands for read only. } . for reading file must exist before if not then use 'w'{write the file} then you dont have to worry abt weathr file is exist or not ,if not it will simply create new file . hope that would help :)

Benjamin • 9 years ago

does hello.txt exist in that folder maybe you should do:
fh=open("hello.txt","w")

Kristian Krejcirk • 8 years ago

file2 = open("newfile2.txt","r")
print file2.read()

invalid syntax? Please help me

Francisco • 10 years ago

Where should the file I am going to open be located in my computer?

أحمد سعيد • 9 years ago

you have to put the file at the directory "C:\Python33 " if you are using python 3.3 or whatever your version is
if you wanna make sure , try to create a new file and you will find at this directory

Deepak Dodeja • 6 years ago

This would work: file = open("G:\\Data sets\\Titanic\\testfile.txt", "r")

awesomeshreyo • 9 years ago

It should be in the same directory as where you have saved the .py file, or otherwise you should type out the entire directory (eg C:\Users\Fransisco\Desktop if your file is saved to the desktop).

FBI • 9 years ago

You must save the file in location where know and then write it and it will be opened and written the information that you want if the code is working

Ikem Krueger • 10 years ago

What's the benefit of using the "with" statement?

James Harvey • 9 years ago

Better syntax and indentation for managing files (In this case/tutorial)

pebre79 • 10 years ago

Thanks for sharing. You have a typos in lines 18 and 29 under File Handling Usages:

write("Hello world")
write("Hello World again")

They should be fh.write(..)

pythonforbeginners • 10 years ago

Fixed. Thank you!

pebre79 • 10 years ago

Thank you for taking time to write these tutorials! :)

Pedro • 10 years ago

Where i put the file to be readed?

SalahAdDinYusuf • 6 years ago

Thank you!

hofffman hofffman • 6 years ago

Can I translate it in Chinese?

Sandy Ma • 6 years ago

You have a lot of typos in your file, and your quotation marks give error many error messages, maybe double check the doc? I used up 3 hours bc of these small typos which are also very hard to detect.

ktappe • 7 years ago

A couple of issues:

1) You have smart quotes around the strings in your file opens. This throws errors if people cut & paste. They must be straight (ASCII-34) quotes to work. No biggie there.

2) The bigger issue is that, at least for me, the strings do NOT get put on separate lines in the text file. They are all put on the same line. I'm using Python 2.7 in macOS. So I guess we have to put "\n" newline characters in?

Gabriel Parker-Gifford • 7 years ago

Yeah i cannot write to different lines. It just writes to the same line.

FBI • 9 years ago

I have tried the code it doesn't working

Kieron Freer • 6 years ago

U need to retype the Speach marks

Xiong Neng • 10 years ago

I like it very much. and I love you too.

mathieusaintes • 5 years ago

print file.readline(3) would not return the third line of the file, but the first 3 characters of the line that would have been returned with file.readline().

To get the third line of a file I thought of file.readlines()[2]

niubi • 5 years ago

Please stop putting those stupid quote charaters: “ ” - they are not valid for any code

the only correct quotes are " or '

Michael Zaki • 5 years ago

how can i delete rows from the text file using python code

FrostBites • 6 years ago

well, beginner here and I didn't learn Python 2x cause it's absolute
I'm using Python 3x , it surely doen't work.

Thanks for the guide, looks like it did work on version 2x

nuri öztürk • 6 years ago

I have a text file like [[ 0.002]
[[0.000444]
how can I read numbers with python

SAO teh sphynx • 6 years ago

im doing a task that is about a hockey game and i need to create a file for each team and have the player names in it (six players)also need to have a file with all the team name an d sop they can piuck that team again so can some 1 write a piece of code that i can partly use for my task and ill mention u into my work

raghu • 6 years ago

how to write in 2 csv sheets in a single csv file

Tuğba Akan • 7 years ago

Thank you

Khang H Nguyen • 7 years ago

Thank you to this article, the article gave me some really good references on file handling subject. If you want to see how I apply file handling, check out one of my article in python that does multiplication that can handle up available space in python, using less than 50 mb of memory foot. The article is at http://kevinhng86.iblog.web...

AL CATRAZ - אל קטרז • 7 years ago

Hey
I want to loop through files in a folder and get the file names for my use within my app...
I want to manipulate the file names and change the name & extension (*.XYZ ie)
How do I do that with PYTHON?

Any links to URLs - or reference to specific commands will help

Thanks

Abdul Basith • 7 years ago

how about others document text? , like .pdf , .doc, etc ??

Kieran • 7 years ago

How do I get my code to recognise the user's inputs and match it to words on a text file?

Prem Anand Chandrasekar • 8 years ago

Is there any way to read and split the file by memory or by number of lines wanted? let say for ex: I have a file which has 10000 lines, I want to read the lines from 10 to 100

kim gihfer • 9 years ago

name = input(' what is your name?: ')
count = input('what is your score?:')
class_no = input('what class are you in?:')

kim gihfer • 9 years ago

name = input('words: ')
count = input('words:')
class_no = input('words:')

marina • 9 years ago

how can i read more than one textfile at the same time without putting them in folder

Suresh • 10 years ago

It's very nice tutorial. But is there any way to extract word by instead of line by line from the file. I mean after getting the entire string we can take it as string and can slice as list but other than this method

Ikem Krueger • 10 years ago

> To open a file for writing use the _built-in_ open() function.

> To append to _a_ file, use:

Typos.

Alexandre Trottier • 10 years ago

Whenever I open a file and use my "for line in file" statement to print the lines of my file, it prints a line, then a blank line, then a line, then a blank one, etc...

pythonforbeginners • 10 years ago

It is possible that your code is well, but the text file you are trying to read can have multiple linebreaks or mixed linebreaks like \n\r. Which operating system do you use?

Alexandre Trottier • 10 years ago

I'm in Windows, but I figured it out! I just had to take out the extra spaces at the end of every line in the text I'm trying to read.

Faustino • 10 years ago

Thanks!

A nice product and a good way to explain.

:-)

pythonforbeginners • 10 years ago

thanks, happy that you liked it :)

Alex P • 6 years ago

You are fucking morons people. Copy paste article much? Have you f tried your code by yourself. You must be chansed out from the community of people who trying to learn.

kim gihfer • 9 years ago

blah blah blah