We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.
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
Hi Atah,
You should give path of file where file does exist.
Eg. fh=open("c://documents/hello.txt","r")
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 :)
does hello.txt exist in that folder maybe you should do:
fh=open("hello.txt","w")
file2 = open("newfile2.txt","r")
print file2.read()
invalid syntax? Please help me
Where should the file I am going to open be located in my computer?
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
This would work: file = open("G:\\Data sets\\Titanic\\testfile.txt", "r")
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).
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
What's the benefit of using the "with" statement?
Better syntax and indentation for managing files (In this case/tutorial)
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(..)
Fixed. Thank you!
Thank you for taking time to write these tutorials! :)
Where i put the file to be readed?
Thank you!
Can I translate it in Chinese?
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.
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?
Yeah i cannot write to different lines. It just writes to the same line.
I have tried the code it doesn't working
U need to retype the Speach marks
I like it very much. and I love you too.
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]
Please stop putting those stupid quote charaters: “ ” - they are not valid for any code
the only correct quotes are " or '
how can i delete rows from the text file using python code
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
I have a text file like [[ 0.002]
[[0.000444]
how can I read numbers with python
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
how to write in 2 csv sheets in a single csv file
Thank you
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...
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
how about others document text? , like .pdf , .doc, etc ??
How do I get my code to recognise the user's inputs and match it to words on a text file?
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
name = input(' what is your name?: ')
count = input('what is your score?:')
class_no = input('what class are you in?:')
name = input('words: ')
count = input('words:')
class_no = input('words:')
how can i read more than one textfile at the same time without putting them in folder
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
> To open a file for writing use the _built-in_ open() function.
> To append to _a_ file, use:
Typos.
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...
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?
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.
Thanks!
A nice product and a good way to explain.
:-)
thanks, happy that you liked it :)
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.
blah blah blah
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?