Python file operations

Source: Internet
Author: User

Tag: Cal class size newline cts als Bubuko tag win

File operations, in the final analysis, there are only two: open files, manipulate files

First, open the file: 文件句柄  =open(‘文件路径‘‘模式‘)

There are two ways to open a file in Python, that is, open (...)and file (...), which in essence calls the latter in order to manipulate files, where we recommend using opento explain

Second, the operation of the document

The action file includes the read, write, and close of the file, first of all to talk about 文件句柄  = open ( ‘文件路径‘ ‘模式‘ Open mode: When we execute) 操作的时候,要传递给open方法一个表示模式的参数:

The mode of opening the file is:

    • R, read-only mode (default).
    • W, write-only mode. "unreadable; not exist; create; delete content;"
    • A, append mode. "Readable; not exist" create; "only append content;"

"+" means you can read and write a file at the same time

    • r+, can read and write files. "readable; writable; can be added"
    • w+, write and reread first. "This method opens the file to empty all the contents of the original file, writes the new content, and then reads what has been written."
    • A +, with a

"U" means that when reading, \ r \ n \ r \ \ \ \ \ \ \ \ \ \ n (note: can only be used with R or r+ mode)

    • RU
    • R+u
    • RbU
    • Rb+u

"B" means processing binary files (e.g. FTP send upload ISO image file , Linux can be ignored, Windows process binary file should be labeled)

    • Rb
    • Wb
    • Ab

The following is the source parsing of the file operation:

  1 class file (object): 2 3 def close (self): # real signature unknown;  Restored from __doc__ 4 close file 5 6 "" Close () None or (perhaps) an integer.  Close the file.  7 8 Sets data attribute. Closed to True.  A closed file cannot is used for 9 further I/O operations.  Close () May is called more than once without error. Some Kinds of File objects (for example, opened by Popen ()) could return an exit status upon closing. "" "" "" Def Fileno (self): # real signature unknown; Restored from __doc__ 15 file descriptor "" "" "Fileno (), integer" File descriptor ". Needed for Lower-level file interfaces, such Os.read (). "" "Return 0 def Flush (self): # real signature unknown;  Restored from __DOC__ 24 flush file Internal buffer "" "Flush () None. Flush the internal I/O buffer. "" "The PASS-def Isatty (self): # real signature unknown;  Restored from __DOC__ 31 determine if the file is consent to the TTY device, "" "Isatty (), True or false. True if the file is connected to a TTY device. "" "", "" "Return False," the Next (self): # real signature unknown; Restored from __doc__ 38 gets the next row of data, does not exist, then the error is "" "X.next (), the next value, or raise stopiteration "" "", "" "The" "," The "Size=none" (Self, the "): # real signature unknown; Restored from __DOC__ 47 read specified byte data "" "Read ([size]), read at most size bytes, returned as a stri Ng. If the size argument is negative or omitted, read until EOF is reached.  Notice that while in non-blocking mode, less data than what is requested-be returned, even if no Size parameter was given. "" " Readinto (self): # real signature unknown; Restored from __doc__ 58 read to buffer, do not use, willAbandoned, "" "Readinto (), undocumented. Don ' t use this; It may go away. "" "", "" "" "" "The ReadLine (Self, size=none): # Real signature unknown; Restored from __DOC__ 66 reads only one row of data "" "ReadLine ([size]), next line from the file, as a string.  Retain newline. A non-negative size argument limits the maximum number of bytes to return (an incomplete line could be returned T hen). Return an empty string at EOF. "" "," "" "The" ReadLines "(Self, Size=none): # Real signature unknown; Restored from __DOC__ 76 read all data and save a list of values based on newline "" "ReadLines ([size]), List of strings, each a line          From the file. ReadLine () repeatedly and return a list of the lines so read. Bayi The optional Size argument, if given, is a approximate bound on the all number of bytes in the Li Nes returned.  "" "The" "[] 85 86 (self, offset, whence=none): # Real signature unknown;  Restored from __DOC__ 89 specifies the pointer position in the file "" "Seek (offset[, whence]), None. Move to new file position.  Argument offset is a byte count. Optional argument whence defaults to the 0 (offset from start of the file, offset should be >= 0); Other values is 1 94 (move relative to current position, positive or negative), and 2 (move relative t  o end of file, usually negative, although many platforms allow seeking beyond the end of a file).  If the file is opened in text mode, the $ offsets returned by tell () is legal. Use of the other offsets causes 98 undefined behavior. The objects is seekable. "" "101 pass102 103 104 (self): # real signature unknown; Restored from __doc__106 gets the current pointer position 107 108 "" "Tell ()-present file position, an integer (could be a long integer). "" "109 pass110 111-def truncate (self, size=none): # Real signature unknown;  Restored from __doc__113 truncates the data, preserving only the previously specified data, "" "Truncate ([size]), None. Truncate the file to in most size bytes.116 117 size defaults to the current file position, as returned by Tell (). "" "118 119 pass120 121 122 123 def write (self, p_str): # Real signature unknown;  Restored from __doc__124 write content 126 "" "Write (str)-None.         Write string str to file.127-Note that due to buffering, flush () or close () may needed before129 The file on disk reflects the data written. "" " 131 pass132 133 def writelines (self, sequence_of_strings): # Real signature unknown;  Restored from __doc__134 writes a list of strings to the file 135 "" Writelines (sequence_of_strings), None.  Write the strings to the file.136 137 Note that newlines is not added. The sequence can is any iterable object138          producing strings. This is equivalent to calling write () for each string. "" "139 pass141 142 143 144 def xreadlines (self): # real signature unknown; Restored from __doc__145 can be used for progressive reading of files, not all 146 147 "" "Xreadlines () returns self.148 149 Fo R backward compatibility. File objects now include the performance150 optimizations previously implemented in the Xreadlines module.           "" "151 Pass

For the above source code in a method, you can look at the actual operation of the use case:

obj1 = open (' Filetest.txt ', ' w+ ') obj1.write (' I heard the Echo, from the valleys and the Heart\n ') obj1.writelines ([' open to The lonely soul of Sickle harvesting\n ',                 ' Repeat outrightly, but also Repeat the well-being of\n ',                 ' eventually swa Ying in the Desert Oasis ']) Obj1.seek (0) print obj1.readline () print Obj1.tell () print obj1.readlines () obj1.close ()

As an example of the open mode of ' w+ ', write is writing a string to the file, and Writelines is writing a string array to the file. The Seek (0) method is to point the pointer to the location, because in the process of writing, the pointer is marked as the content of the write continues to move, theseek method can move the pointer to the specified position , and this time point to the 0 position, starting from this position to read, You can read everything you've just written; ReadLine () reads a line from the pointer position, so here the execution ReadLine reads the first line in the file just written, and the tellis the current position of the pointer , and this is the time to execute the Say () method. The pointer points to the starting position of the second row, and the subsequent ReadLines method reads the remainder of the file after the current pointer into the array as rows. Is the result of the file and console after the program executes:

Although just using the ' w+ ' way to open the file, but in fact this open way in file processing is not commonly used, was once rated as ' meaningless ' by our teacher, because the ' w+ ' method will empty the original file of everything ~

The above breath introduced so many methods, let us have a general concept, and then the various functions of these methods to take out the comparison:

Write file operations

Write,writelines, compared to those multifarious reading methods, writing method is more simple, only Wite and writelines two species. Look at the following example and the result of the write, in fact, the Write method and the Writelines method are similar, except that an accepted parameter is the list format, an accepted argument is a string format. Note the newline character when used here.

1 obj1 = open (' E:\PythonL\\11-8\\filetest.txt ', ' R ') 2 obj1 = open (' Filetest.txt ', ' w+ ') 3 obj1.write (' I heard the Echo, from The valleys and the Heart\nopen to the lonely soul of Sickle harvesting\n ') 4 obj1.writelines ([5                  ' Repeat outrightly, b UT also repeat the well-being of\n ', 6                  ' eventually swaying in the desert Oasis ' 7                  ])

We have just used the write and Writelines method to write a small poem of Tagore to the file, the result is as follows:

I heard the echo, from the valleys and the Heartopen to the lonely soul of sickle harvestingrepeat outrightly, but also re Peat the well-being ofeventually swaying in the desert oasis

Read File operation

Let's take this document as an example to read the file:

First look at the method of reading all the contents of the file directly read and ReadLines, from the following results to know the two methods of a return list, one is the return string, and the above write-method corresponds to:

1 #readline方法2 obj1 = open (' E:\PythonL\\11-8\\filetest.txt ', ' R ') 3 print ' ReadLines: ', Obj1.readlines () 5 #readline方法6 Print "read:", Obj1.read ()
1 readlines: [' I heard the Echo, from the valleys and the heart\n ', ' Open to the lonely soul of Sickle harvesting\n ', ' Rep Eat outrightly, but also repeat the well-being of\n ', ' eventually swaying in the desert Oasis ']
1 Read:i heard the echo, from the valleys and the Heart2 Open to the lonely soul of sickle harvesting3 Repeat outrightly, But also repeat the well-being of4 eventually swaying in the desert oasis

The ReadLines and read methods are simple and easy to use, but if the file is large, a one-time read-in memory slows down the performance of the program, and at this point we need a line of read files to reduce the memory usage.

Readline,next,xreadlines: Used to read a file by line, which requires a careful look at the use of Xreadlines, because Xreadlines returns an iterator and does not return the contents of a row directly

It's important to note that while I'm putting this big piece of code together, I'm going to get an error (valueerror:mixing iteration and read methods would lose data) if I really put this whole bunch of stuff together. The specific reasons are explained below.

1 obj1 = open (' E:\PythonL\\11-8\\filetest.txt ', ' R ') 2 #readline方法 3 print "ReadLine:", Obj1.readline () 5 #readline方法 6 Prin T "Next:", Obj1.next () 8 #readline方法 9 r = obj1.xreadlines () print ' Xreadlines: ', R.next () #readline方法13 print ' ReadLine S: ', Obj1.readlines () #readline方法16 print "read:", Obj1.read ()

Let's start by showing the results of executing the above programs:

The left is the code, and the right side is the corresponding execution result. Here we show the three methods of Readline,next,xreadlines.

1 Readline:i heard the echo, from the valleys and the Heart2 3 Next:open to the lonely soul of sickle Harvesting4 5 Xrea Dlines:repeat outrightly, but also Repeat the well-being of

One thing to add here is that the Xreadlines method was deprecated after python3.0, and it was replaced by a direct traversal of the FOR statement:

1 obj1 = open (' Filetest.txt ', ' R ') 2 for line in Obj1:3     print line 4  5 run Result: 6 I heard the echo, from the valleys And the Heart 7  8 Open to the lonely soul of sickle harvesting 9 Repeat outrightly, but also Repeat the well-being Of11 eventually swaying in the desert oasis

A pointer in a file

After reading the file read and write, the basic operation of the file we solved, the following describes the file processing and pointers related methods: Seek,tell,truncate

1 obj1 = open (' Filetest.txt ', ' w+ ') 2 obj1.write (' I heard the Echo, from the valleys and the heart\n ' 3 ' open to the lonely s  Oul of Sickle harvesting\n ') 4 print ' 1.tell: ', Obj1.tell () 5 obj1.writelines ([6                  ' Repeat outrightly, but also Repeat the Well-Being of\n ', 7                  ' eventually swaying in the desert Oasis ' 8                  ]) 9 print ' 2.tell: ', Obj1.tell ()

First of all, see Tell,tell's role is to indicate where the current pointer is located. Regardless of whether the file is read or written, it depends on the position of the pointer, we start reading from the position of the pointer, and start writing from the position of the pointer. Let's write the results of the tell in the middle of the previous content. After executing the code, the results are as follows:

1.tell:962.tell:188

Then look at the use of seek:

1 obj1 = open (' E:\PythonL\\11-8\\filetest.txt ', ' R ') 2 print "Next:", Obj1.next (), ' tell1: ', Obj1.tell (), ' \ n ' 3 Obj1.seek ( 4 print "read:", Obj1.read (), ' tell2: ', Obj1.tell (), ' \ n '

From the execution results shown, we use the Tell method when we use next to read the file, this time the return is 188, the pointer has pointed to the end of the tell (for specific reasons explained below), then we execute the Read method, the content is not available, This time we use the Seek method to point the pointer to the 50 position, and then use the Read method, you can read the rest of the content.
Looking at an example of truncate:

1 obj1 = open (' Filetest.txt ', ' r+ ') 2  3 Obj1.write (' This is a truncate test,*** ') 4 obj1.seek (0) 5 print ' first read:\n ', Obj1.read () 6  7 obj1.seek (0) 8 Obj1.write (' This is a truncate test ') 9 obj1.truncate () obj1.seek (0) print ' \nsec Ond read:\n ', obj1.read ()
1 First Read:2 This was a truncate Test,***valleys and the Heart3 Open to the lonely soul of sickle Harvesting4 Repeat Outr ightly, but also repeat the well-being of5 eventually swaying in the desert OASIS6 7 second Read8 This is a truncate test

With the above print results, we can know that in the file to write operations, the position of the pointer will be directly covered by the corresponding content, but many times after we modify the file, the following things do not want to retain, this time we use the Truncate method, the file will only save the current pointer position before the content. We can also use TRUNCATE (n) to hold the content before N, where n represents the pointer position.

With Operation files

To avoid forgetting to close after opening a file, you can manage the context by using the: with open (' File path ', ' Operation mode ') as file handle:

1 #使用whith打开可以不用close2 with open (' E:\PythonL\\filetest.txt ', ' R ') as File_obj:3     file_obj.write (") 4 5 #在Python 2.7 after , with and support for managing the context of multiple files simultaneously, the following example opens two files at a time
6 #with Open (' E:\PythonL\\filetest1.txt ', ' R ') as File_obj1,open (' E:\PythonL\\filetest2.txt ', ' W ') as File_obj2: '

Errors that are easy to make:

Valueerror:mixing iteration and Read methods would lose data

I have encountered such a problem in the process of manipulating files, which literally means that the pointer is wrong, so how does this problem arise? I found that this error occurs after using the next or Xreadlines method and then using the Read or ReadLines method, because next or xreadlines includes the way we normally use the For loop to read the file. The program maintains a pointer within itself (which also explains how the pointers we get with the Tell method when we use these methods are all pointing at the end of the file, not the current unique location), so if we want to read a line first using the next or Xreadlines method above, Then use the Read or ReadLines method to read the rest of the content will be an error.

Solution:

There are two types of solutions at this time:

First, after reading a line, using seek to specify the position of the pointer, you can continue to use other methods

The second, using the ReadLine method, this method does not have internal maintenance of the pointer, it is spicy simple line of silly reading, the pointer will be a silly line down to move. This time you can also use the Tell method to track the correct position of the pointer, you can also use the Seek method to locate the location, with Truncate,wirte and other methods, can not be better use some.

Python file operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.