Python file operations,
File objects in python:
File objects can be used not only to access common disk files, but also to access any other types of abstract files.
". Once an appropriate" Hook "is set, you can access other objects with file type interfaces, as if accessing
Same as a file.
File built-in functions [open () and file ()]
1 open('filename')2 with open('filename') as f:3 pass
The open function uses a file name as a unique mandatory parameter, and then returns a file object.. Both mode and buffer parameters are optional. The default mode is read-only mode. Use with to close the file even if an error occurs,
The following lists the access modes of file objects:
1 file mode operation 2 r open 3 rU in read-only mode or Ua in Read mode, and provide support for general line breaks (PEP 278) 4 w open in write mode (empty if necessary) 5 a open in append mode (create a file starting from EOF and when necessary) 6 r + open 7 w + in read/write mode (see w) 8 a + open in read/write mode (see a) 9 rb open in binary read mode 10 wb open in binary write mode (see w) 11 AB open in binary append mode (see) 12 rb + Enabled in binary read/write mode (see r +) 13 wb + Enabled in binary read/write mode (see w +) 14 AB + Enabled in binary read/write mode (see a +) 15 x if the file exists and an error does not exist, create 16 *** and B mode to open the file. You cannot specify encoding. You also need to convert it to byte write file 17 during data splitting.18 below are some examples of opening files:19 f = open ('/etc/motd') # open 20 f = open ('test', 'w') in Read mode ') # open 21 f = open ('data', 'r + ') in write mode # open 22 f = open ('IO. sys ', 'rb') # enable the 23 24 bytes string in binary read mode to convert the byte type 25 n = bytes (Li Jie, encoding = 'utf-8') [converted characters, can be a variable, encoded after conversion] 26 bytes converted to string 27 str (bytes (Li Jie, encoding = 'utf-8'), encoding = 'utf-8) 28 File Operations
The open () and file () functions have the same functions and can be replaced at will. you can use file () to replace open. we recommend that you use open () to read and write files and use file () when processing file objects (). After open () is successfully executed and a file object is returned, all subsequent operations on the file will be performed using this "handle.
File methods can be divided into four types: input, output, file movement, and miscellaneous operations.
Input
Read ()The method is used to directly read bytes to the string. A maximum of a specified number of bytes can be read. If no size is specified
If the parameter (default value:-1) or size value is negative, the file will be read until the end.
Read size:
Readline ()Method to read the line of the open file (read all the bytes before the end of the next line). Then the whole line, including the row
Returns the string. It is the same as read (). It also has an optional size parameter. The default value is-1, which indicates that
The row Terminator. If this parameter is provided, an incomplete row is returned after the size exceeds the limit.
Readlines ()The method does not return a string like the other two input methods. It reads all (remaining) rows.
And return them as a string list. Its optional parameter sizhint indicates the maximum size of the returned bytes. If it is large
At 0, all returned rows should have approximately sizhint bytes (which may be slightly larger than this number, because the buffer needs to be pooled.
Size ).
Output
Write ()The built-in method function is opposite to read () and readline ().
The string is written to the file.
File Movement
Seek ()(Similar to the fseek () function in C), you can move the file pointer to different positions in the file. offset
Byte represents the offset relative to a certain position. The default value of the position is 0, indicating that the value starts from the beginning of the file (that is, the absolute offset), 1 generation
2 indicates that the table is counted from the current position. If you are a C programmer and have used fseek (),
0, 1, and 2 correspond to the constants SEEK_SET, SEEK_CUR, and SEEK_END respectively. When people open files for read and write operations
You will be exposed to the seek () method.
# Skip the first five characters and read the following content
Truncate ()Method, which accepts an optional size as the parameter. If given
The part is truncated to the maximum size byte. If the size parameter is not passed, the part is truncated to the current position of the file by default.
For example, if you open a file and call the truncate () method immediately, your file (content) is actually deleted,
At this time, you actually extract from 0 bytes (tell () will return this value)
Others:
Close ()
Close the file to end access to it. The Python garbage collection mechanism will also reduce the reference count of the file object.
The file is automatically closed when the file is zero. This occurs when only one file is referenced, for example, f = open (...), then f in the original
An object is assigned to another file before it is explicitly disabled. Good Programming habits require that the object be closed before another file object is assigned.
If you do not close the file explicitly, you may lose the data in the output buffer.
Fileno ()Method to return the descriptor for opening the file. This is an integer and can be used in OS module (OS. read ().
Some underlying operations
Flush ()Instead of passively waiting for the output buffer.
Write. isatty () is a Boolean built-in function. If the file is a tty-like device, True is returned; otherwise
False. The truncate () method intercepts the file to the current file pointer location or to the given size, in bytes.
List of built-in methods of file objects
1File object operations2 file. close () close file 3 file. fileno () returns the file descriptor (file descriptor, FD, integer) 4 file. flush () refresh the file's internal buffer 5 file. isatty () determines whether the file is a tty-like device 6 file. nexta () returns the next line of the file (similar to file. readline (), or if there are no other rows, 7 causes a StopIteration exception 8 file. read (size =-1) reads size bytes from the file. If no size is specified or a negative value is given, read 9 to get all the remaining bytes and return them as strings. if the open mode does not have B, read by character; otherwise, read 10 files by byte. readline (size =-1) reads from the file and returns a row (including the row Terminator), or returns the maximum size11 file = open ('test', 'AB') 12 file. write (bytes ('hei', encoding = 'utf-8') to write a string to the file (will overwrite the original data, pitfall 13 file. close () close file 14 file. tell obtains the position of the current pointer 15 file. read () if the open mode does not have B, read by character, otherwise read by byte 16 file. fileno file descriptor 17 file. truncat. seek (off, whence = 0) moves the file pointer in the file, from whence (0 indicates the beginning of the file, 1 indicates the current position, 2 indicates the end of the file) offset off byte, whether or not there are Chinese characters, all are in bytes 19 file. truncate (size = file. tell () captures the file to the maximum size byte. The default value is the current file position of 20 files. writelines (seq) writes string sequence seq to the file; seq should be a 21 iteratable object 22 file that returns the string. readlines (sizhint = 0) reads all the rows of the file and returns the result as a list (including 23 characters after all rows );
Access to the file system
Most access to the file system is implemented through the Python OS module, which is the main function of Python to access the operating system.
Interface.
OS module File/directory access function
File Processing
1 mkfifo ()/mknod () a create named pipeline/Create file System Node 2 remove ()/unlink () Delete file 3 rename ()/renames () b. Rename the file 4 * statc (). Return the File Information 5 symlink (). Create the symbolic link 6 utime (). Update the timestamp 7 tmpfile (). Create and open it ('W + B ') a new temporary file 8 walk () a generates all file names under a directory tree
Directory/folder
1 chdir ()/fchdir () a changes the current working directory/change the current working directory through a file descriptor 2 chroot () d changes the root directory of the current process 3 listdir () listing objects in a specified directory 4 getcwd ()/getcwdu () a returns the same current working directory/function, but returns a Unicode object 5 mkdir ()/makedirs () create directory/create multi-level directory 6 rmdir ()/removedirs () delete directory/delete multi-level directory
Access/permission
1 access () Check permission Mode 2 chmod () Change permission Mode 3 chown ()/lchown () a change owner and group ID/function same, but do not track Link 4 umask () set the default permission Mode
File descriptor operations
1 open () underlying operating system open (for files, use the standard built-in open () function) 2 read ()/write () read/write data according to file descriptor 3 dup ()/dup2 () copy file description symbol/function is the same, but is copied to another file descriptor
Device No.
1 makedev () a creates an original device number from the major and minor device numbers 2 major () a/minor () a get the major/minor device number from the original device number
OS. pathYou can complete some operations on the path name. The functions provided by this function can complete the management and operations.
Obtain file or sub-directory information, file path query, and other operations.
Path Name access function in the OS. path Module
Separate
1 basename () Remove directory path, return file name 2 dirname () Remove file name, return directory path 3 join () combine the separated parts into a path name 4 split () return (dirname (), basename () tuples 5 splitdrive () returns (drivename, pathname) tuples 6 splitext () returns (filename, extension) tuples
Information
1 getatime () returns the last access time 2 getctime () returns the file creation time 3 getmtime () returns the last file modification time 4 getsize () returns the file size (in bytes)
Query
1 exists () specifies whether the path (file or directory) exists 2 isabs () specifies whether the path is an absolute path 3 isdir () specifies whether the path exists and is a directory 4 isfile () specifies whether the path exists and whether the path exists for a file 5 islink () and a symbolic link 6 ismount () to specify whether the path exists and is a mount point 7 samefile () whether the two pathnames point to the same file
There are a large number of other modules related to files and input/output, most of which can work on mainstream platforms.
1 base64 encoding/decoding between binary strings and text strings 2 binascii encoding/decoding between binary and ASCII strings 3 bz2a access BZ2 compressed files 4 csv access csv files (separated by commas) 5 filecmpb is used to compare directories and files 6 fileinput provides row iterators for multiple text files 7 getopt/optparsea provides Command Line Parameter Parsing/processing 8 glob/fnmatch provides Unix-style wildcard matching function 9 gzip/zlib: Read and Write GNU zip (gzip) files (compression requires the zlib module) 10 shutil provides advanced file access functions 11 c/StringIO provides class file interfaces for string objects 12 tarfilea reads and writes TAR archive files, supports compressing file 13 tempfile to create a temporary file (name) 14 uu format encoding and decoding 15 zipfilec tool for reading ZIP archive files
Below are some simple examples:
Iterate on files without using variables to store file objects
1 for line in open(finename):2 print(line)
Open two files at the same time
1 with open('user.txt') as f,open('user1.txt') as f2:2 pass
Obtain the first 10 rows of Files
1 with open('db1', 'r', encoding="utf-8") as f1, open("db2", 'w',encoding="utf-8") as f2:2 times = 03 for line in f1: 4 times += 1 5 if times <=10:6 f2.write(line)7 else:8 break
Replace the specified string in the file
1 with open('db1', 'r', encoding="utf-8") as f1, open("db2", 'w',encoding="utf-8") as f2:2 for line in f1:3 new_str = line.replace("alex", 'st')4 f2.write(new_str)