Hope to continue to update through the blog park, share and record the basic knowledge of Python to the advanced application of the dots drop!
11th Wave: Chapter 11th Documents and materials
This chapter went a step further and allowed the program to reach more areas: files and streams. The functions and objects described next allow you to store data for program invocation and to process data from other programs.
[11.1] Open file
The open function is used for opening the file with the following syntax:
Open (Name[,mode[,buffering]])
The open function uses a file name as the only mandatory argument, and then returns one of the document objects. The modes mode and buffer buffering parameters are optional.
If you have a text file named Somefile.txt whose storage path is C:\text, you can open the file as follows.
>>>f=open (R ' C:\text\somefile.txt ')
Later, you'll look at the usefulness of the file object, and first consider the other two parameters of the open function.
[11.1.1] File mode
If the open function takes only one file name argument, you can get a file object that can read the contents of the file. If you want to write content within a file, you must provide a schema parameter to explicitly declare it. The mode parameter in the Open function has only a few values, as follows:
R Read mode
W Write mode
A append mode
b binary model (can be added to other modes for use)
+ read/write mode (can be added to other modes for use)
It is not necessary to explicitly formulate a read pattern and what mode parameters are not used for the same effect. Use write mode to write content like a file. The ' + ' parameter can be used in any other mode, and the fatal read and write are allowed. For example, ' r+ ' can be used when opening a text file for reading and writing.
The ' B ' mode changes the way the file is processed, and in general, Python assumes that it handles text files (including characters). However, if you are dealing with some other type of file (a binary file), such as a sound clip or an image, you should add ' B ' to the pattern parameter. The parameter ' RB ' can be used to read a binary file.
Python's way of working with text files is surprising, and the only trick you'll need to do is standardize line breaks. In general, a newline character (\ n) in Python indicates the end of a line and another line, which is also a specification in UNIX systems. But the end of the line in Windows is the identity \ r \ n. In order to hide these differences in the program, Python does some automatic conversions here: When reading text from a file in text mode under Windows, Python converts \ r \ n to \ n. Conversely, when you write text to a file in text mode under Windows, Python converts \ n to \ r \ n.
A problem may arise when using binary files, because the file contains bytes that can be interpreted or referred to as the newline character, while using text mode, Python automatically converts it, but this destroys the binary data, so in order to avoid this happening, use binary mode.
Note that by using the U parameter in a modal parameter, you can use a common line feed support pattern when opening a file, in which case all line breaks/strings are converted to \ n, regardless of the platform being run.
11.1.2 Buffering
The Open function has 3 parameters (optional) to control the buffering of the file. If the parameter is 0 (or false), I/O is unbuffered (all read and write operations are directed to the hard disk), and if 1 (or True), I/O is buffered (meaning Python uses memory instead of the hard disk to make the program faster, Only flush or close will be used to update data on the hard disk. Numbers greater than 1 represent the size of the buffer (in bytes), and 1 (or any negative number) represents the use of the default buffer size.
[11.2] Basic file method
The way to open a file has been introduced, and the next step is to use them to do something useful. Here are some basic methods for file objects (and some class file objects, sometimes called streams). The class file object is an object that supports methods of some files, such as the file method, and more importantly, the read or write method. The objects that have urllib.urlopen return are a good example. The methods they support are read, ReadLine, and ReadLines.
[11.2.1] Read and write
The most important ability of a file (or stream) is to provide or accept data, and if you have a class file object named F, you can write and read data using the F.write method and the F.read method.
each time you call F.write (String), the supplied argument string is appended to the part that already exists in the file.
>>>f=open (' Somefile.txt ', ' W ')
>>>f.write (' Hello. ')
>>>f.write (' world! ')
>>>f.close ()
When you are finished working on a file, call close. Reading is simple, just remember to tell the stream how many characters (bytes) to read:
>>>f=open (' Somefile.txt ', ' R ')
>>>f.read (4)
' Hell '
>>>f.read ()
' O. world! '
The number of characters to read is first set at 4, and then the remaining files are read (by not providing the string to be read). You can omit the schema description when calling open, because ' R ' is the default.
[11.2.2] Tubular output
In the Unix shell, you can use a pipeline to continue writing additional commands after one command, as in the following example:
$cat Somefile.txt | Python somescript.py | Sort
This pipeline pipeline consists of the following 3 commands:
Cat Somefile.txt: Just write the contents of Somefile.txt to standard output (sys.stdout);
Python somescript.py: This command runs the Python script somescript.py. The script should read from the standard input and write the results to the standard output.
Sort: Reads all text from standard input (Sys.stdin), sorts alphabetically, and then writes the result to standard output.
What is the role of the pipe symbol (|)?
The pipe symbol connects the standard output of a command with the standard input of the next command. In this way, it is known that Somescript will read the data from its Sys.stdin and write the result into its sys.stdout.
Random access: The examples in this chapter manipulate files as streams---that is, you can only read data in the order they are from beginning to end. In fact, it is also possible to move the read position freely in a file, using the class file object seek and tell to directly access the part of interest (this is called Random access):
Seek (Offset[.whence]): This method moves the current position (where it is read and written) to the location defined by offset. Whence.offset is a number of bytes. Whence default is 0, which means that the offset is calculated from the beginning of the file. The whence may be set to 1 (as opposed to the current position) or 2 (relative to the end of the file movement).
[11.2.3] read-write line
It is also not a problem to read a file by character, but it is also possible to read rows by line. You can use File.readline to read a single line (starting at the current position until a newline character appears and reading the newline character). Do not use any parameters (so that a row is read and returned) or use a nonnegative integer as the maximum value of the characters that the ReadLine can read. Therefore, if Somefile.readline () returns ' hello,world!\n ', Somefile.readline (5) returns ' Hello '. The ReadLines method can read all the rows in a file and return them as a list.
The Writelines method and ReadLines are reversed: A list of strings is passed to it, which writes all the strings to the file. Note that the program does not add new lines and needs to be added yourself.
[11.2.4] Closing files
Remember to close the file using the Close method. You can avoid useless modifications in some operating systems or settings, which also avoids the use of quotas for files opened in the system. Files that have been written should always be closed because Python may cache data that is written (temporarily stored in place for efficiency). If the program crashes for some reason, the data is not written to the file at all. For security purposes, close the file after you have finished using it.
# Open your file here
Try
# Write data to your file
Finally
File.close ()
In fact, there is a statement specifically designed for this case, the WITH statement:
With open ("Somefile.txt") as Somefile:
Do_something (Somefile)
The WITH statement can open a file and assign it to a variable, in this case the somefile. The data can then be written to the file in the body of the statement. The file is automatically closed after the statement ends, even if the exception causes the end.
From __future__ import with_statement
After writing some file content, the general idea is that these changes will be immediately reflected in the file. This allows other programs that read this file to know about the changes. Isn't that right?
Not necessarily! The data may be cached (temporarily stored in memory), knowing that the closed file will not be written to the file. If you need to continue using the file and want to update the files on the disk to reflect those changes, then call the Flush method of the file object.
[11.2.5] Using the basic file method
Suppose Somefile.txt contains the following, what can I do with it?
The first is read (n):
F=open (R ' C:\text\somefile.txt ')
>>>f.read (4)
>>>print F.read ()
>>>f.close ()
followed by ReadLine ():
>>>for I in range (3):
Print str (i) + ': ' +f.readline ()
and ReadLines ():
>>>import Pprint
>>>pprint.pprint (Open (R ' C:\text\somefile.txt '). ReadLines ())
Here is the write file, the first is write (str):
>>>f=open (R ' C:\text\somefile.txt ', ' W ')
>>>f.write (' This\nis No\nhaiku ')
>>>f.close ()
Finally, Writelines (list):
>>> F=open (R ' c:\text\somefile.txt ')
>>>lines=f.readlines ()
>>>f.close ()
>>>lines[1]= "isn ' t a\n"
>>>f=open (R ' C:\text\somefile.txt ', ' W ')
>>>f.writelines (lines)
>>>f.close ()
[11.3] iterating over the contents of the file
The methods that are provided by some text objects of the file object are described earlier, and how to obtain such a file object. It is one of the most common file operations to iterate over the contents of a file and perform some operations repeatedly. Some of these techniques are using methods that have been seen (such as read, ReadLine, ReadLines), and other methods that are about to be introduced (such as xreadlines and file iterators).
def process (string):
print ' Processing: ', string
[11.3.1] Processing by byte
The most common way to iterate over the contents of a file is to use the Read method in a while loop. For example, to loop through each character:
F=open (filename)
Char=f.read (1)
While Char:
Process (char)
Char=f.read (1)
F.close ()
This program can be used because the Read method returns an empty string when the end of the file is reached. But the string that was returned before that would contain a single character (so the Boolean value is true). The improved version code is as follows:
F=open (filename)
While True:
Char=f.read (1)
If not char:break
Process (char)
F.close ()
[11.3.2] action by row
When working with text files, you often iterate over the lines of a file instead of processing a single character. The processing line uses the same method as the processing character, which is the ReadLine method, with the following code:
F=open (filename)
While True:
Line=f.readline ()
If not line:break
Process (line)
F.close ()
[11.3.3] Read all content
If the file is not very large, you can use the Read method without parameters to read the entire file at once, or use ReadLines. Note that it is also useful to read the contents of a file into a string or a read-in list at other times. The following code:
F=open (filename)
For Char in F.read ():
Process (char)
F.close ()
For line in F.readlines ():
Process (line)
F.close ()
[11.3.4] using Fileinput to implement lazy line iterations
When you need to iterate over a very large file, the readlines consumes too much memory. You can use the For loop to use a method called lazy line iteration, which says that it is lazy because it simply reads the part of the file that is actually needed.
Import Fileinput
For Lin in fileinput.input (filename):
Process (line)
In legacy code, lazy line iterations can be implemented using Xreadlines. It works in a similar way to readlines, except that it does not say that all lines are read into the list but instead create a Xreadlines object. It is best to use Fileinput or file iterators in your own code.
[11.3.5] File iterator
In nearly several versions of Python, file objects are iterative. This means that they can be used directly in the for loop to iterate over them. The following code:
F=open (filename)
For line in F:
Process (line)
F.close ()
Look at the following example:
>>>lines=list (Open (' somefile.txt '))
>>>lines
[' first line ', ' second line ', ' third line ']
>>>first,second,third=open (' Somefile.txt ')
In this example, it is important to note the following points:
Use Print to write content to the file, which adds a new line after the provided string;
Use a sequence to unpack an open file, placing each row in a separate variable;
The file is closed after the file is written to ensure that the data is updated to the hard disk.
[11.4] Summary
class File object: class file object supports the informal object of the Read and ReadLine methods.
Open and close a file: open a file by providing a filename.
patterns and file types: When you open a file, you can provide a pattern.
Standard Flow :
Read and write: use the Read or write method to read or write to a file object or class file object.
Read- Write lines: use the ReadLine and ReadLines and Xreadlines methods to read rows from a file, using Writelines to inhale data.
Iterate file contents
Basic Python Tutorial "reading notes"-2016/8/3