Discussion:
[Tutor] File operation query
Reuben
2015-10-15 16:00:09 UTC
Permalink
Hi All,

I need some clarification for below code. In line 2 of below code snippet,
I have provided read and write permission. Assuming I have provided some
string input as requested in line 1 - when I try to open "check.txt" file
after running the script, it is always empty - it does not display the user
input provided.

May I know why?

######################################################################

input1 = raw_input("Input1:")


file = open("check.txt", "r+")

file.write(input1 + "\n")


for line in file:
print line


print file.close()

######################################################################


Regards,
RD.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Peter Otten
2015-10-15 16:30:46 UTC
Permalink
Post by Reuben
I need some clarification for below code. In line 2 of below code snippet,
I have provided read and write permission. Assuming I have provided some
string input as requested in line 1 - when I try to open "check.txt" file
after running the script, it is always empty - it does not display the
user input provided.
May I know why?
I don't know the details, but the problem is the caching mechanism used to
speed up file iteration.
Post by Reuben
input1 = raw_input("Input1:")
file = open("check.txt", "r+")
file.write(input1 + "\n")
print line
print file.close()
I use the rule of thumb that file iteration and other methods are
incompatible in Python 2.

You may be able to make it work (in this case a file.flush() before the for
loop seems to do the job), but I prefer not to bother.


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Martin A. Brown
2015-10-15 17:32:54 UTC
Permalink
Hello and good day Reuben,
Post by Peter Otten
Post by Reuben
I need some clarification for below code. In line 2 of below code
snippet, I have provided read and write permission. Assuming I
have provided some string input as requested in line 1 - when I
try to open "check.txt" file after running the script, it is
always empty - it does not display the user input provided.
May I know why?
I don't know the details, but the problem is the caching mechanism
used to speed up file iteration.
I find Peter's comment interesting. I did not know this.
Post by Peter Otten
Post by Reuben
input1 = raw_input("Input1:")
file = open("check.txt", "r+")
file.write(input1 + "\n")
print line
print file.close()
I use the rule of thumb that file iteration and other methods are
incompatible in Python 2.
You may be able to make it work (in this case a file.flush()
before the for loop seems to do the job), but I prefer not to
bother.
I would like to make two additional comments:

Unless you are operating on many (many) files, then, why not call
close() on the file and reopen. Until performance concerns dominate
(possibly, never), this should offer you the predictability you
expect, without worrying about file pointer location.

If you are using Python 2, then don't call your variable 'file'.
The word 'file' in Python 2 behaves like open

file("check.txt", "r+")
# means the same thing as:
open("check.txt", "r+")

I would, therefore write your program like this:

input1 = raw_input("Input1:")

f = open("check.txt", "w")
f.write(input1 + "\n")
f.close()

f = open("check.txt", "r")
for line in f:
print line
f.close()

Good luck!

-Martin

[0] https://docs.python.org/2/library/functions.html#file
--
Martin A. Brown
http://linux-ip.net/
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Joel Goldstick
2015-10-15 16:32:02 UTC
Permalink
Post by Reuben
Hi All,
I need some clarification for below code. In line 2 of below code snippet,
I have provided read and write permission. Assuming I have provided some
string input as requested in line 1 - when I try to open "check.txt" file
after running the script, it is always empty - it does not display the user
input provided.
May I know why?
######################################################################
input1 = raw_input("Input1:")
file = open("check.txt", "r+")
file.write(input1 + "\n")
print line
print file.close()
file.close()

Do you have a traceback? Can you add that to your question?
Post by Reuben
######################################################################
Regards,
RD.
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
--
Joel Goldstick
http://joelgoldstick.com/stats/birthdays
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Emile van Sebille
2015-10-15 16:47:40 UTC
Permalink
Post by Reuben
Hi All,
I need some clarification for below code. In line 2 of below code snippet,
I have provided read and write permission. Assuming I have provided some
string input as requested in line 1 - when I try to open "check.txt" file
after running the script, it is always empty - it does not display the user
input provided.
May I know why?
######################################################################
input1 = raw_input("Input1:")
file = open("check.txt", "r+")
file.write(input1 + "\n")
try inserting file.flush() here

Emile
Post by Reuben
print line
print file.close()
######################################################################
Regards,
RD.
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Emile van Sebille
2015-10-15 16:49:28 UTC
Permalink
Post by Reuben
Hi All,
I need some clarification for below code. In line 2 of below code snippet,
I have provided read and write permission. Assuming I have provided some
string input as requested in line 1 - when I try to open "check.txt" file
after running the script, it is always empty - it does not display the user
input provided.
May I know why?
######################################################################
input1 = raw_input("Input1:")
file = open("check.txt", "r+")
file.write(input1 + "\n")
Also, the line below continues from the last insertion -- you'll also
need to reposition the file pointer or re-open the file.

Emile
Post by Reuben
print line
print file.close()
######################################################################
Regards,
RD.
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-10-15 17:35:12 UTC
Permalink
Post by Reuben
I need some clarification for below code. In line 2 of below code snippet,
I have provided read and write permission.
Thios is nearly always a bad idea and leads to all sorts of
complications, as you are discovering!
Post by Reuben
string input as requested in line 1 - when I try to open "check.txt" file
after running the script, it is always empty - it does not display the user
input provided.
Its not empty but you are positioned after the text you inserted.
Post by Reuben
input1 = raw_input("Input1:")
file = open("check.txt", "r+")
file.write(input1 + "\n")
This writes your text at the start of the file
(incidentally overwriting anything that was there
from the previous run). It leaves the file cursor
at the end of your txt.
Post by Reuben
print line
This tries to read from the file cursor to the end of the file.
But you are already at the end so it returns nothing.
You need to do a seek(0) to put the cursor back to the start.
Then remember to go back to the end before trying to write again.
I told you it was more complicated!
Post by Reuben
print file.close()
printing close() is not useful.

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Reuben
2015-10-16 07:52:59 UTC
Permalink
Thanks everyone for the suggestion.

At the moment, file.flush() resolves the problem - but I will also give a
thought to suggestions provided by other members in this email chain.

Regards,
RD
Post by Reuben
I need some clarification for below code. In line 2 of below code snippet,
Post by Reuben
I have provided read and write permission.
Thios is nearly always a bad idea and leads to all sorts of complications,
as you are discovering!
string input as requested in line 1 - when I try to open "check.txt" file
Post by Reuben
after running the script, it is always empty - it does not display the user
input provided.
Its not empty but you are positioned after the text you inserted.
Post by Reuben
input1 = raw_input("Input1:")
file = open("check.txt", "r+")
file.write(input1 + "\n")
This writes your text at the start of the file
(incidentally overwriting anything that was there
from the previous run). It leaves the file cursor
at the end of your txt.
Post by Reuben
print line
This tries to read from the file cursor to the end of the file.
But you are already at the end so it returns nothing.
You need to do a seek(0) to put the cursor back to the start.
Then remember to go back to the end before trying to write again.
I told you it was more complicated!
print file.close()
printing close() is not useful.
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Loading...