Python provides the necessary functions and methods for basic file operation by default
How to open the file:
Open (Name[,mode[buf]]) Name: File path mode: Open mode buf: buffer buffering size
File read mode:
Read ([size]): Read file (read size byte, read all by default)
ReadLine ([size]): reads a row
ReadLine ([size]): Read buffer buf (IO. Default_set_buffer), returns the list of each row
ITER: Using an iterator to traverse the Read file F.open (name); iter_f = iter (f); Using the for line in Iter_f Loop iterator
With open (' pi_digits.txt ') as F: # Default mode is ' R ', read-only mode
Contents = F.read () # reads the entire contents of the file
File Write Method:
Write (str): Writes a string to a file
Writelines (sequence_of_strings): Writes multiple lines to a file, with arguments as objects that can be iterated
When write (str) is called, the Python interpreter calls the system call to write the contents to disk, but the Linux kernel has a file caching mechanism, so caches the kernel buffers, and when close () or flush () is called, the content is actually written to the file
or write -in data volume greater than or equal to write cache, write cache will also be synchronized to disk
Purpose of closing the file
1: Write cache sync to disk
The number of open files per process in the 2:linux system is limited
3: If the number of open files to the system limit, the open file will fail
Python file pointer operation:
Seek (Offset[,whence]) move the file pointer
Offset: Offsets, can be a negative number
Whence: Offset relative position
How the Python file pointer is positioned:
Os. Seek_set start position relative to the file 0
Os. Seek_cur relative to the current position of the file 1
Os. Seek_end position relative to end of file 2
Python file properties:
File.fileno (): File descriptor;
File.mode: File open permission;
File.encoding: File encoding method;
File.closed: Whether the file is closed;
Python Standard Files:
Standard input file: Sys.stdin; Read-only descriptor is 0
Standard output file: sys.stdout; Write-only descriptor is 1
Standard error file: Sys.stderr; Write-only descriptor is 2
Python command-line arguments:
The SYS module provides the SYS.ARGV property, which allows you to get command-line arguments. SYS.ARGV is a string sequence that holds the command-line arguments, where sys.argv[0] is the file name and 1~n is the true argument
Python File Encoding method
The default encoding format for Python files is ASCII format, to be written in Chinese you can convert the encoding format
1. A = Unicode.encode (U ' hello ', ' utf-8 ') conversion, a Chinese character occupies 3 bytes in ASCII code and 2 bytes in Unicode.
2. Create a file in utf-8 format directly. Create the specified encoding format file using the methods provided by the codecs module:
Codecs.open (fname, mode, encoding, errors, buffering): Open a file using the specified encoding format
3. You can also create a file in the specified encoding format using the system-provided open () function:
Open (file, mode= ' R ', Buffering=-1, Encoding=none, Errors=none, Newline=none, Closefd=true, Opener=none)
Simple Linux File system
Here is the process for Python action files
.python file access 1. In Python to access the file, the first to open the file, that is, openr: read-only w: write only, the file already exists is emptied, does not exist create a: Append, write to the end of the file. If the file exists, go to chase add at the end of the file. File does not exist to create +-: update (Readable writable) r+: Open in read/write mode w+: Open in read/write mode (see W) A + : Open in read-write mode (see a) RB: Open WB in binary read mode: Open in binary write mode ab: Open in binary append mode (see a) rb+: Open in binary read/write mode (see r+) wb+: Open in binary read-write mode (see w+) ab+: Open in binary read/write mode (see A +) 2. Open the file. Open opening file read reading file, close file import codecsfd = Codecs.open (' 2.txt ') print Fd.read () fd.close () >>> 11111222233333aaaaabbbbbcccccc 3. What are the methods of viewing a file import CODECSFD = Codecs.open (' b.txt ') print fd.read () print dir (FD) Fd.close () >>> 111112222333333[' close ', ' Closed ', ' encoding ', ' errors ', ' Fileno ', ' flush ', ' isatty ' , ' mode ', ' name ', ' newlines ', ' next ', ' read ', ' Readinto ', ' readline ', ' readlines ', ' seek ', ' softspace ', ' Tell ', ' truncate ' , ' write ', ' writelines ', ' Xreadlines '] 1>fd.read () method, the Read () method reads the entire document. &NBSP;FD = Codecs.open (' 2.txt ') Text = Fd.read () print type (text) >>><type ' str ' >&NBSP;&NThe Bsp;2>replace () function replaces an element in the file. Opens the file, reads it, and then operates on the entire string. Replace 1 in 2.txt file with Z&NBSP;FD = Codecs.open (' 2.txt ') a1 = Fd.read () Print a1a2 = A1.replace (' 1 ', ' Z ') print A2 >>> 11111222233333aaaaabbbbbcccccc zzzzz222233333aaaaabbbbbcccccc 3> Write files, Codecs.open () function, avoid file garbled fd = Codecs.open (' 3.txt ', ' W ') fd.write (' liuzhenchuan\n ') fd.write (' Hello world\n ') Fd.write (' xiaban\n ') fd.close () >>> Liuzhenchuanhello worldxiaban 4>fd.readlines () method, Read the file, and finally put the contents of each line of the file as a string in a list &NBSP;FD = open (' 3.txt ') print fd.readlines () fd.close () >>> [' liuzhenchuan\n ', ' Hello world\n ', ' xiaban\n '] 5>fd.readline () method, read file, read file line, type string >>> l 6> #fd. ReadLine () method, reads the file line, returns a string . # Fd.next () method, reads the next line of the file, returns a string fd = Codecs.open (' 3.txt ', ' R ') print Fd.readline () print fd.next () fd.close () >>> liuzhenchuan Hello world 7> #write () method, mustMust pass in a string. fd = Codecs.open (' 5.txt ', ' w+ ') fd.write (' a\nb\nc\n ') fd.close () >>> a b c #writelines () method, must pass in a list/sequence FD = Codecs.open (' 6.txt ', ' W ') fd.writelines ([' 123\n ', ' 234\n ', ' 345\n ']) fd.close () >>> 123 234 345 8 >with usage, do not need to use Fd.close () to close the file with Codecs.open (' 3.txt ', ' RB ') as fd: print fd.read () Fd.close () >>> liuzhenchuanhello worldxiaban 9> Print file line number and file contents with Codecs.open (' 2. TXT ') as fd: for Line,value in Enumerate (FD): print line, value, >>> 0 liuzhenchuan 1 Hello world 2 xiaban 10> Filter the contents of a file line with Codecs.open (' 3.txt ') as fd: for Line,value in Enumerate (FD): & nbsp; if line = = 3-2: Print value >>> Hello world 11> import the Linecache module, using the Linecache.getline () method, Get the contents of a fixed line of files import linecachecount = Linecache.getline (' 3.txt ', 1) print count >>> Liuzhenchuan
Read and write operations for python--files