One, the file object
I understand that a file object is an interface that operates on a file through this interface.
"Python core Programming" is very obscure, there is no deep understanding, I hope someone can explain to me.
>>> f = open (' Demo.txt ', ' R ') >>> f<open file ' demo.txt ', mode ' r ' at 0x00cccec0>>>> type ( f) <type ' file ' >
Second, correlation function
[1], built-in function: Open ()
Provides a common interface for initializing input/output (I/O) operations, which returns a file object when a file is successfully opened, or an ioerror exception occurs. The syntax is as follows:
File_object = open (File_name,access_mode = ' r ', buffering =-1)
The analysis is as follows:
1): file_name: A string that represents the name of the file to open, either a relative or an absolute path.
For example, I create a new text file with a blank name of out in the D drive and then do the related operation.
>>> f = open ('.. /out.txt ', ' W ') >>> f.write (' good ') >>> f.close ()
When the file is opened, it is found that ' good ' has been written in.
Next, use the absolute path for access, as follows:
>>> f = open (' D:/out.txt ', ' R ') >>> F.read () ' Good ' >>> f.close ()
2. Optional variable Access_mode is also a string that represents the file open mode.
Attention:
1): Files opened using ' R ' or ' U ' mode must already exist
2): The ' W ' mode opens the file if it exists, then empties it and then re-creates
3): ' A ' mode open files are prepared for appending data, and all written data is appended to the end of the file. If the file does not exist, it is created automatically.
4): ' + ' stands for readable writable
5): ' B ' stands for binary mode access
6): By default is ' R '
3. Buffering optional parameters that indicate the caching method used to access the file. 0 means no cache; 1 means only one row is cached, and n is the cache n rows. If not provided or negative, the system default caching mechanism is used.
Use? This trouble someone can tell me!
To close a file:
Open files consume system resources and depend on file mode, and other programs may not be able to access them. This is where it is important to close the file once the operation is complete.
After the above operation, the join has not been closed:
>>> f.closed #判断是否关闭False >>> f.close () #关闭方法 >>> f.closedtrue>>> F.tell () #如果已关闭, everything is invalid for file operations Traceback (most recent call last): File "<pyshell#27>", line 1, in <module>
[2], factory function file ()
As with open (), you can replace each other
>>> f = File ('.. /out.txt ', ' R ') >>> F.read () ' Good ' >>> f.close ()
Iii. Related methods
Return a File object (handle), in fact I do not know what is the handle, here from the Baidu Encyclopedia:
Handle, which is the basis for the entire Windows programming. A handle refers to the use of a unique integer value, that is, a four-byte long value, to mark different objects in the application and similar objects in different instances, such as a window, buttons, icons, scroll bars, output devices, controls or files, and so on. The application is able to access the information of the corresponding object through a handle, but the handle is not a pointer, and the program cannot use the handle to directly read the information in the file. If the handle is not in the I/O file, it is useless. A handle is a unique integer that Windows uses to flag an application or use a large number of handles to flag many objects.
General file-related methods are: input, output, in-file move, miscellaneous
[1], input
1, read (): Read bytes into a string, there is an optional parameter size, the default is-1, if 1 or a complex number then the file will be read to the end of the file.
>>> f.read () ' Good ' >>> f.read (2) ' Go '
2, ReadLine (): Reads a line of the file, including the line terminator. The same as read () also has an optional parameter size.
3, ReadLines (): Read all (remaining) and then return them as a list of strings, it has an optional parameter sizhint represents the maximum word size returned.
>>> f = File ('.. /out.txt ', ' R ') >>> f.readlines () [' Good ']
[2], output
1. Write (): writes a string containing text data or binary data blocks to a file.
2, Writelines (): For the list operation, accept a list of strings as parameters, write them to the file, the line terminator is not automatically added, if necessary, you must call the Writelines method before the end of each line to add a terminator.
>>> f = open (' D:/out.txt ', ' W ') >>> f.writelines ([' Man ']) >>> f.close () >>> f = open (' D:/out.txt ', ' R ') >>> F.read () ' Man '
"Note:"
When reading rows from a file using read () or readlines (), Python does not delete the line terminator, which is left to the programmer.
>>> f.readlines () [' Game is your life,game are your life,game is your life,game are your life,game is your life,[*]< c0>\n', ' game is your life,game are your life,game is your life[] ']--------------------------->>> f = open (' d Emo.txt ', ' r ') >>> data = [Line.strip () for line in F.readlines ()]>>> data[' game are your life,game is your Life,game is your life,game are your life,game is your life,[*] ', ' game was your life,game is your life,game is your life[] ‘]
Similarly, write () and writelines () do not automatically join the line terminator and should be added yourself.
>>> f = open (' Demo.txt ', ' W ') >>> f.writelines ([' My name is beginman\n,i like coding ']) >>> f.cl OSE ()
Open the file to view:
[3], moving within the file
Tell (), Seek () method
f = open (' D:/out.txt ', ' RB ') print F.tell () #[1]f.seek (10,1) #[2]print F.tell () #10print f.read () # Beginman,print f.read #I like Coding.print F.tell () #38 "" "1": Tell (): Tell Method confirms that a file object has been moved to the current file location to maintain the state of the file it opens. The Tell method of the file object tells you the current position in the open file. Because we haven't done anything about this file, the current position is 0, which is where the file starts. "" "" "" "2": Seek (): The Seek method of the file object moves to another location in the open file. The second parameter indicates what the first parameter means: 0 to move to an absolute position (from the beginning of the file), 1 to move to a relative position (from the current position), and 2 for the end of the file "" "
[4], file iteration
>>> f = open (' D:/out.txt ', ' RB ') >>> for eachline in F: eachline ' My name is beginman,\r\n ' I lik E coding.\r\n '
Iv. related attributes
File.closed: The file has been closed, otherwise false
File.mode: File access mode
File.name: File name
>>> F.mode ' r ' >>> f.name ' demo.txt '
From:http://www.cnblogs.com/beginman/p/3166644.html
Python action on a file (go)