Study notes for file operations.
>>> file = open ('/tmp/test.txt ', ' R ')
>>> File.read ()
' Root:x:0:0:root:/root:/bin/bash\nbin:x:1:1:bin:/bin:/sbin/nologin\ndaemon:x:2:2:daemon:/sbin:/sbin/nologin\ nadm:x:3:4:adm:/var/adm:/sbin/nologin\nlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\nsync:x:5:0:sync:/sbin:/bin/ Sync\nmysql:x:27:27:mysql server:/var/lib/mysql:/bin/bash\n '
>>> File.close () #文件使用完以后要关闭文件 because the file consumes resources
>>> with open ('/tmp/test.txt ', ' R ') as F:
... print (F.read ())
... #with语句自动调用了close方法
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Sync:x:5:0:sync:/sbin:/bin/sync
Mysql:x:27:27:mysql Server:/var/lib/mysql:/bin/bash
Read () reads all content at once, the file is too large, the memory is prone to problems
Read (size) size Specifies the maximum number of bytes to read at a time and is more secure
ReadLine () reads one line at a time
If the file is small, Read () One-time reading is most convenient, if the file size cannot be determined, repeated calls read (size) Compare insurance; If it is a configuration file, call the ReadLines () Most Convenient:
#f = open ('/users/michael/gbk.txt ', ' R ', encoding=' GBK ', Errors= ' Ignore ', newline= ')
Open can pass in some parameters:
Encoding: Specify encoding
Errors: The easiest way to deal with the error is to ignore ignore it, but it's not good to use it too much.
NewLine = "': python can recognize all normal line breaks and convert them to a single \ n character. Similarly, the newline character \ n is converted to the system default newline character at output. If you do not want this default processing, you can give the open () function The parameter newline= "
>>> with open ('/tmp/hello.txt ', ' W ') as F:
... f.write (' Hello ') #文件写入
... print (' Hello ', file=f) #print输入重定向到文件中
...
>>> File.read ()
' Hello hi \ n '
#--File Basic Operations
Output = open (R ' C:\spam ', ' W ') # Opens the export file for writing
input = open (' Data ', ' R ') # opens a file for reading. Open the way can be ' W ', ' R ', ' A ', ' WB ', ' RB ', ' AB ' and so on
Fp.read ([size]) # size is the length of the read, in bytes
Fp.readline ([size]) # reads a line, if a size is defined, it is possible to return only part of a row
Fp.readlines ([size]) # takes each line of the file as a member of a list and returns the list. In fact, its internal is through the Loop call ReadLine () to achieve. If the size parameter is supplied, the size is the total length of the read content.
Fp.readable () # is readable
Fp.write (str) # writes STR to a file, write () does not add a newline character after Str
Fp.writelines (SEQ) # Writes the contents of the SEQ to the file (multiline write-once)
Fp.writeable () # is writable
Fp.close () # closes the file.
Fp.flush () # Write the contents of the buffer to the hard disk
Fp.fileno () # Returns a long integer "file label"
Fp.isatty () # file is a terminal device file (Unix system)
Fp.tell () # Returns the current position of the file action tag, starting with the origin of the file
Fp.next () # returns to the next line and shifts the file action marker to the next line. When a file is used for a statement such as for ... in file, it is called the next () function to implement the traversal.
Fp.seek (Offset[,whence]) # Moves the file action marker to the location of offset. Whence can be calculated from scratch for 0, and 1 for the origin at the current position. 2 means that the end of the file is calculated as the origin.
Fp.seekable () # Is it possible to seek
Fp.truncate ([size]) # Cuts the file to the specified size, and the default is the position of the current file action tag.
For line in open (' Data '):
Print (line) # using the For statement, the comparison applies to open larger files
Open (' f.txt ', encoding = ' latin-1 ') # python3.x Unicode text file
This article is from the "unplug the Operational Space" blog, please be sure to keep this source http://zhangdj.blog.51cto.com/9210512/1881951
Python3_05. File operations those things