Python file I/O

Source: Internet
Author: User

One, the process of operating the file:

First, set up the file object.

Second, invoke the file method to operate.

Third, don't forget to close the file. (If the file is not closed, the content is placed in the cache, although Python will automatically read the content to disk at the end, but in case of the habit of closing the file)

eg:f = open (' 1,txt ', encoding = ' utf-8 ')

Print (F.read ())

F.close ()

Two, a full list of open files in different modes:

Note: If the file does not exist, it will be an error when it is stained with R.

Just dip the W and clear the contents of the file.

Pointer positions in different modes and different modes:

Third, file Operation method

F.seek (n)  #查看光标定位
F.tell () #查看当前文件中指针的位置
F.truncate () #清空文件内容
f = open (' file1,txt ', encoding = ' utf-8 ')
Print (F.tell ()) #光标默认在起始位置
F.seek (#把光标定位到第10个字符之后)
Print (F.tell ()) #输出10
F.close ()
Common operations:
f = open (' file.txt ', ' A + ')

F.seek (0) #把光标定位到起始位置
F.truncate () #清空文件内容
F.read () #读取文件里面所有的内容, do not use large files, because the contents of the file will be read into memory, memory is not enough to burst memory
F.readline () #读取一行的内容
F.readlines () #读取所有文件内容, return a list, the element is the data of each row, do not use the large file,
Because the contents of the file will be read into memory, memory is not enough, will be the memory burst
F.readable () #判断文件是否可读
F.write () #写入内容 Pass in a str type when writing
F.writelines () #str list dic tuple type (when writing, pass in an iterative object)
Large file, read file efficient operation method:

f = open (' file.txt ')
For line in F:
Print (line)
In this case, line is the content of each row of files, after reading a line, it will release a row of memory

with use: in the operation of the file, often forget to close the file, so that you can use with, it will be used after the file handle, automatically close the file, using the following way:

&NBSP; < Span class= "crayon-i" >with open ( File.txt ' , ' R ' )   as f: #打开一个文件 , pay the handle of this file to F

For line in F: print(line)    with open ( ' file.txt ' as fr,with open ( File_bak ' , ' W ' ) Span class= "crayon-h" > as FW : #这个是多文件的操作, open two files, Fr is read FILE.TXT,FW is a new File_bak file            for line in Fr: #循环file. txt for each row FW. Write(line)#写到file_bak文件中   

Python file I/O

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.