Issue with the File I/O module : reading between the lines


#1

Continuing the discussion from BUG - It keeps saying please refresh the page:

Please see the issue in the screenshot attached. I changed the text.txt and removed the changes and re-executed the code but to no avail! Kindly assist as I am not able to complete the course otherwise.


#2

@nishant.khanolkargma,

Try using

print my_file.readline()

#3

My bad!! Thanks a lot! :slightly_smiling: That resolves my issue.


#4

Now Let's see:

  1. Make a List that contains the Text it should contain
  2. Open the File and write the Text into it and Close the File
  3. Open the File again to read the lines (and Close the File)

It is kinda cheating --> Somehow like this:

# Lets "Cheat" thru this one for now!
my_list = ("I'm the first line of the file!\n"
"I'm the second line.\n"
"Third line here, boss.\n")
# So we first, make a List that contains the text it should!
# Now write the list it into the textfile
my_file = open("text.txt", "r+")
for i in my_list:
    my_file.write("%s" %i)
my_file.close()
# Now the list works ;)
# And Finally the Code needed to pass this one:
my_file = open("text.txt", "r")
print my_file.readline()
print my_file.readline()
print my_file.readline()
my_file.close()
# Et violà :) now i don't get any error messages!

Because, if everything is written correctly it still says it's not


#5

that fixed it for me, thanks!


#7

I struggled my self with this one. Because I had the coding done right but it always says I have to refresh the browser page over and over again. But still couldn't pass. So I had the idea, to create the List my self first and test if it works. AND IT DID :smile: happy me :smile:


#8

I have that problem too. How do you create the file?


#9

Nvm figured it out myself just use "w+" instead of "r" as the second argument.


#10

Here is my snippet and it's not working for me


#11

@rahulsoni4u,
With
my_file = open("text.txt","r")

you are trying to open an existing file.....in read modus.....
If you use following code
you will see which files are available....

import os
print "==show us the existing files =="
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
    print str(f)
#my_file.close()

==================================================

With following code
you will do a test of the existence of text.txt file
and if the file text.txt is not found
you will create the text.txt file

import os
print "== if the file doesn't exist, create one =="
if os.path.exists('text.txt'):
    print "File exists"
else:
    print "Creating the text.txt-file"
    my_file = open("text.txt", 'w')
    my_file.write("I'm the first line of the file!" +"\n")
    my_file.write("I'm the second line."+"\n")
    my_file.write("Third line here, boss."+"\n")
    my_file.close()

my_file = open("text.txt","r")
print my_file.readline()
print my_file.readline()
print my_file.readline()
my_file.close()

Reference:
== discussions / opinions ==
http://stackoverflow.com/questions/11968976/list-files-in-only-the-current-directory
http://stackoverflow.com/questions/18533621/creating-a-new-text-file-with-python
http://stackoverflow.com/questions/5214578/python-print-string-to-text-file


#12

Add some more Code :wink:

# 1st - Create a list
my_list = ("I'm the first line of the file!\n"
"I'm the second line.\n"
"Third line here, boss.\n")

# 2nd - Open a file and import the list
my_file = open("text.txt", "r+")
for i in my_list:
    my_file.write("%s" %i)
my_file.close()

# 3rd - Now here comes your code!
my_file = open("text.txt", "r")
print my_file.readline()
print my_file.readline()
print my_file.readline()
my_file.close()

# 4th - Done

#13

:slight_smile:

Thanks for the help @nskilly

I did indeed created the file first and then read it!

Cheers! :beers:


#14

Thank you @leonhard.wettengmx.n


#15

A post was split to a new topic: Writing to file


#16