Road Python Python file operations

First, the operating files

File handle = open ( 'file path + file name', 'mode')

example

f = open("test.txt","r",encoding = “utf-8”)

 

Analysis: This is due to the python files and test.txt file in the same folder, no need to write the absolute path test

If you want to write the absolute path can be written

f = open(file = "d:/python/test.txt","r",encoding = “utf-8”)

 

There are several files open mode mode

 

1, the text file open mode

"R", the default mode [read-only mode, the file must exist, there is no exception is thrown]

"W", write-only mode [unreadable; does not exist, create; there is then emptied the contents]

"A", only additional write mode [unreadable; does not exist, create; there is only the additional content]

Is opened in a binary file, the read byte is the type of content, but also a need to provide byte type writing, you can not specify the encoding

"+" Indicates a file can read and write at the same time

"R +", [reader readable, writable]

"W +", [read write read, write}

"A +", [read write read, write}

2、readable()、writable()

readable's () determines whether the file read, return True or False,

Are writable () whether a file is writable return True or False,

example

f.readable()

f.writable()

 

3、read()、readline() 、readlines()

f.read () # read all of the content, move the cursor to the end of file

Returns the result of a string, read([size])a method to read the file size bytes from the current position, if no size parameter, indicates to the file until the end of the reading

 

f.readline () # read a single line, the cursor moves to the second row header

Result is returned as a string

 

f.readlines () # read each line content, stored in the list

Read all text content, each line as an element of a list, and returns the results in a list format, but read large files would be more total memory.

 

4、write()、writelines( )

write () to write the string

writelines () can pass both strings and can pass a sequence of characters, and writes files in the sequence of characters. Note that you must pass a sequence of characters, it can not be a sequence of numbers.

f.write ( '1111 \ n222 \ n ') # write for text mode, you need to write their own newline 
f.write ( '1111 \ n222 \ n'.encode (' utf-8 ')) # mode for b write, write their own needs newline 
f.writelines ([ '333 \ n' , '444 \ n']) # file mode 
f.writelines ([bytes ( '333 \ n', encoding = 'utf-8'), '444 \ n'.encode (' utf- 8 ')]) #b mode

 

  

5, f.close () # close the file f.closed () # to view the file is closed

After opening the file with the open method, you must use f.close () closes the file

After the file must be closed after use, because the file object will occupy the resources of the operating system and the operating system at the same time the number of files that can be opened is limited.

Whether f.closed () # View the file is closed, return True or False

6, with open as f open method

This opens the file the way to close the file without writing f.closed

example

with open('/path/to/file', 'r') as f:
with  open("test.txt","r",encoding = “utf-8”) as f:

 

7, for non-text files, we can only use the b mode

Non-text files open mode, only the mode b, "b" represents the manner in bytes (and all the files are stored in the form of bytes, the use of this model without considering the character of the encoded text document, jgp format image files, avi format video files, b mode cross-platform)

"rb"

"wb"

"ab"

f = open("test.py","rb")

 

Analysis: Note that this can not be applied, encoding = "utf-8", because there is a binary opened, do not need to set the encoding open

 

example

f = open("test.py","rb")
data = f.read()
print(data)

 

 

  

test.py contents of the file:

Export

b'"111"\r\n"2222"\r\n"3333"\r\n\xe4\xbd\xa0\xe5\xa5\xbd'

 

Analysis: Here \ r \ n is the newline Windows platform, beginning with b represents the output byte form

 

Here \ xe4 \ xbd \ xa0 \ xe5 \ xa5 \ xbd 'on behalf of Chinese characters

f = open("test.py","rb")
data = f.read()
print(data.decode("utf-8"))

 

  

Output

 

"111"
"2222"
"3333"
你好

 

  

 

Analysis: as used herein when test.py file storage utf-8 is stored, to "utf-8" to be decoded at the time of output printout.

 

 

When opened by b, the read byte is the type of content, but also a need to provide byte type writing, you can not specify the encoding

Example 2

f = open("test11.py","wb")f.write("111\n")
 
f.close()

 

  

Output: Error, suggesting bytes needed here, instead of a string of bytes written here must be written in the form

 

Can be changed

f = open("test11.py","wb")
f.write(bytes("111\n",encoding="utf-8"))
f.close()

 

  

Analysis: here you can string "111 \ n" is written files, where bytes () must be assigned a code.

 

Or directly encoded string, not bytes () method

 

f = open("test11.py","wb")
f.write("222\n".encode("utf-8"))
f.close()

 

  

 

8、f.encoding

Take open files encoded

example

f = open("test11.py","w",encoding="gbk")
f.write("222\n")
f.close()
print(f.encoding)

 

  

Export

gbk

 

  

Analysis: where the file is open coding that open statements in the code, regardless of the actual encoding of the source file.

 

 

9、f.flush() 、f.tell()

f.flush () immediately file contents from memory to the hard brush, where the need to use the command prompt operation, will be written directly to write the contents of the hard disk pycharm years, do not need to flush ()

f.tell () Gets the current cursor position

 

10, the cursor moves in the paper

 

read(3):

When a, as a way to open the file in text mode, read on behalf of three characters

B, the file open mode is a mode b, three bytes represent the data read

read () reads the entire file default

The remaining files are cursor movement bytes as seek, tell, truncate

example

test.txt file contents

f = open("test.txt","rb")
data = f.read(6)
f.close()
print(data)

 

  

Output

b'111 \ r \ n2 '

 

Analysis: This 3-byte count 111, \ r \ n 2 byte count, a byte count 2, where b is the mode for calculation byte, i.e. there can not specify the encoding can not be written encoding = "xx ", otherwise it will error

 

Example 2

File contents same as above

f = open("test.txt","r+",encoding="utf-8")
data = f.read(6)
f.close()
print(data)

 

  

Output

111
22

 

  

Analysis: This is a text mode, where the operator 3 111, newline \ r \ n a character count, count 2 22 characters, a total of six characters.

 

11、seek()

seek () move the cursor to the file to the specified location.

seek () syntax

  f.seek(offset[, whence])

i.e., the number of bytes offset movement, 0,1,2 The whence there are three modes, 0 represents the beginning of the file count, count represents the current position, represents the start end of the file count, the default is 0. 1 and 2 wherein the b mode must be carried out, but no matter what the wrap mode, the mobile units are in bytes of, the Widnows system size is two bytes (\ r \ n).

example

test.txt file contents

 

f = open("test.txt","r+",encoding="utf-8")
f.seek(3,0)
data = f.read()
f.close()
print(data)

 

  

Output

复制代码
222 
333 
Hello 
444 
aaa 
bbb 
ccc 
555
复制代码

 

  

Example 2

File contents same as above

复制代码
f = open("test.txt","rb")
f.seek(5)
f.seek(11,1)
print(f.tell())
​
data = f.read()
f.close()
print(data)
复制代码

 

  

 

Output

16
 
b'\xbd\xa0\xe5\xa5\xbd\r\n444\r\naaa\r\nbbb\r\nccc\r\n555'

 

  

 

analysis

 

 

Here in binary open, three-byte count 111, behind unseen \ r \ n newline, therefore, seek (5) the position of the first row is the last

seek(11,1)是从当前位置继续移动光标,即222\r\n算5个字节,同理333\r\n算5个字节,“你好”(文本文件是utf-8编码的)算6个字节,因此这时只取“你”这个字的3个字节的第一个字节,光标移动到你的第一个字节之后,所以最后输出了\xbd\xa0\xe5\xa5\xbd共5个字节。

注意这里的光标操作要用seek()方法,直接用鼠标移动光标是无效的。

 

 

例子3

文件内容

 

复制代码
f = open("test.txt","rb")
offs = -20
while True:
    f.seek(offs,2)
    data = f.readlines()
    if len(data) > 1:
        print("文件的最后一行是%s"%data[-1].decode("utf-8"))
        breakf = open("test.txt","rb")
offs = -20
while True:
    f.seek(offs,2)
    data = f.readlines()
    if len(data) > 1:
        print("文件的最后一行是%s"%data[-1].decode("utf-8"))
        break
复制代码

 

  

这里目的是在不知道一行是多少字节的情况下输入最后一行

输出结果

文件的最后一行是2018-04-15 nicholas 学习了520分钟

 

分析:seek(-20,2)是从文件的最后开始计算的,必须以b模式进行。

 

12、 truncate()

truncate() 方法用于从文件的首行首字节开始截断,截断文件为 size 个字节,无 size 表示从当前位置截断;截断后面的所有字节被删除,其中 Widnows 系统下的换行代表2个字节大小。

 

例子

文件内容

 

f = open("test.txt","r+")
f.truncate(10)
data = f.read()
print(data)

 

  

输出结果

111
 
aaa

Guess you like

Origin www.cnblogs.com/QaStudy/p/11514907.html