Python file operations

I got the error "IOError: [Errno 0] Error" with this python program:

from sys import argv file = open("test.txt", "a+") print file.tell() # not at the EOF place, why? print file.read() # 1 file.write("Some stuff will be written to this file.") # 2 # there r some errs when both 1 & 2 print file.tell() file.close() 

What is the problem? These two cases are below:

 from sys import argv file = open("test.txt", "a+") print file.tell() # not at the EOF place, why? # print file.read() # 1 file.write("Some stuff will be written to this file.") # 2 # there r some errs when both 1 & 2 print file.tell() file.close() 

and

 from sys import argv file = open("test.txt", "a+") print file.tell() # not at the EOF place, why? print file.read() # 1 # file.write("Some stuff will be written to this file.") # 2 # there r some errs when both 1 & 2 print file.tell() file.close() 

still why

 print file.tell() # not at the EOF place, why? 

Doesn't print file size, is it "+" in add mode? should the file pointer point to EOF?

I am using Windows 7 and Python 2.7.

+13
source share
2 answers

Python uses the stdio fopen function and passes the mode as an argument. I assume you use windows, as @Lev says the code works fine on Linux.

Below is the fopen windows documentation, this may be the key to solving your problem:

When the access type is "r +", "w +" or "a +", both read and write are allowed (the file is considered open for "update"). However, when you switch between reading and writing, there should be fflush, fsetpos, fseek or rewind. An electric current position can be indicated for the operation fsetpos or fseek, if desired.

So the solution is to add file.seek() before calling file.write() . To add to the end of the file, use file.seek(0, 2) .

For your reference, file.seek works like this:

To reposition file objects, use f.seek (offset, from_what). The position is calculated by adding an offset to the reference point; the breakpoint is selected with the from_what argument. A from_what value of 0 measures from the beginning of the file, 1 uses the current position of the file, and 2 uses the end of the file as a control point. from_what can also be omitted by default 0, using the beginning of the file as a starting point.

[link: http://docs.python.org/tutorial/inputoutput.html]

As mentioned in the comments of @lvc and @Burkhan in his answer, you can use the new public function from io module . However, I want to note that the write function does not work exactly the same in this case - you need to specify unicode strings as input [just a u prefix for the string in your case]:

 from io import open fil = open('text.txt', 'a+') fil.write('abc') # This fails fil.write(u'abc') # This works 

Finally, please avoid using the name β€œfile” as the variable name, since it is an inline type and will silently overwrite, which leads to some errors that are difficult to detect.

+10
source

The solution is to use open from io

 D:\>python Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> f = open('file.txt','a+') >>> f.tell() 0L >>> f.close() >>> from io import open >>> f = open('file.txt','a+') >>> f.tell() 22L 
+6
source

Source: https://habr.com/ru/post/955630/


All Articles