Python-based file processing

Source: Internet
Author: User

First, the common operation

1. Opening the file open ()

F=open (' A.txt ')
Print (f) #----------------------> <_io. Textiowrapper name= ' a.txt ' mode= ' r ' encoding= ' cp936 ' >

Where A.txt is a relative path, if you want to open the file with an absolute path, add a ' r ' before the file name, otherwise you will change ' \ ' to ' \ \ '

There is a problem with opening the file, which is the problem of character encoding. If you do not specify an open encoding, the default is to use the operating system's encoding, Windows is GBK

Here you can see how the file is encoded, what encoding is used to save the file, what encoding to use to open the file, you can specify an open encoding when opening the file

Written like this: F=open (' a.txt ', encoding= ' utf-8 ')

Another problem is that this way of opening the file will read the entire file into memory, if the file is very large, the memory may be full, you can use for the way in F

After opening the file, you can manipulate the file, such as read the file F.read (), note that at the end must close the file F.close ()

2. Open mode of the file

F=open (file path, mode)

The mode of opening the file is:

    • R, read-only mode "default mode, file must exist, not present, throw exception"
    • W, write-only mode "unreadable; not exist" created; empty content "
    • X, write-only mode "unreadable; not present, create, present error"
    • A, append mode "readable; not present, create; append content only"

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

    • r+, read and write "readable, writable"
    • w+, write "readable, writable"
    • x+, write "readable, writable"
    • A +, write "readable, writable"

"B" means to operate in bytes

    • RB or R+b
    • WB or W+b
    • XB or W+b
    • AB or A+b #以字节的方式操作不会有乱码, no transcoding

3.print (F.readline ()) print the first line of the file

4.print (F.readlines ()) print files as a list

5. Open the file as read and cannot manipulate the file, only open the file in the write mode

F.open (' A.txt ', ' W ', encoding= ' utf-8 ')

If the file exists, first delete the original file contents, then write

If the file does not exist, create a new file and write

In writing, it is important to note that the file is a string type, with quotation marks, or in the form of a list, but each element of the list must also be a string

F=open (' A.txt ', ' W ', encoding= ' utf-8 ')
F.write (' 123413\n ') #\n represents line break
F.write ([' 123\n ', ' hello\n '])
F.close () #注意最后一定要关闭文件

Ii. Other methods

F=open (' A.txt ', ' W ', encoding= ' utf-8 ')
F.flush () #写完后立即刷到硬盘上

Print (F.close ()) #查看文件是否为关闭状态

Print (f.encoding) #查看编码方式

Print (f.name) #查看文件名

Print (F.readable ()) #查看是否可读

Print (F.wirteable ()) #查看是否可写

Print (F.read ()) #括号里可以指定数字, only the number in read () is in characters,
# The rest of the files within the cursor movement, such as seek,tell,truncate, are in bytes

Print (F.seek (2)) #seek是以字节为移动单位, but Chinese characters are in three bytes in Utf-8, so if Seek (2) it will be an error.

Print (F.tell ()) #告诉当前光标位置

F.truncate (3) #截断, delete from 0 to 3, note that it must be written.

Copy files in three or two binary ways

Take a picture of a.jpg as an example and copy

Read_file=open (' a.jpg ', ' RB ')
Write_file=open (' a.copy.jpg ', ' WB ')
Write_file.write (Read_file.read ())

Iv. Modification of documents

#修改文件a. txt ' hello ' for ' Hi '
Import OS #导入os模块
With open (' A.txt ', ' R ', encoding= ' Utf-8 ') as Read_f,\ #用with语句可以很方便的进行上下文处理, can automatically close the operation of the file
Open ('. A.txt.swp ', ' W ', encoding= ' Utf-8 ') as Write_f: #新建一个临时文件
For line in Read_f:
If ' Hello ' in line:
Line=line.replace (' Hello ', ' Hi ')
Write_f.write (line)
Os.remove (' A.txt ')
Os.rename ('. A.txt.swp ', ' a.txt ')

Python-based file processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.