Basic operations for Python Learning---files

Source: Internet
Author: User

The operation of the file is divided into three steps:

1, open the file to get the handle of the file, the handle is understood as this file

2. Manipulating files through file handles

3, close the file.

Basic file Operation:

    • f = open (' file.txt ', ' R ') #以只读方式打开一个文件, gets the file handle, if it is read, R can not write, default is read-only,
    • In Python2 there is also a file method to open the files, Python3 in the document method is no longer, only open
    • Frist_line = F.readline () #获取文件的第一行内容, returns a list
    • Print (Frist_line) #打印第一行
    • res = F.read () #获取除了第一行剩下的所有文件内容
    • Print (RES)
    • F.close () #关闭文件

When opening a file, you need to specify the file path and how to open the file, open it to get the file handle, followed by this file handle to the file operation, open the file mode is:

    • R, read-only mode (default).
    • W, write-only mode. "unreadable; not exist; create; delete content;"
    • A, append mode. "unreadable; not exist; create; append content only;"

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

    • r+, "Readable, writable, append, if open file does not exist, will error"
    • w+, "write-in mode, use w+ words, the existing file content will be emptied, can read the contents of the written file"
    • A +, "append read/write mode, create without existing, append content only;"

File Operation Method:

  • f = open (' file.txt ', ' r+ ', encoding= ' utf-8 ') #encoding参数可以指定文件的编码
  • F.readable () #判断文件是否可读
  • Fr.writable () #判断文件是否可写
  • fr.encoding# encoding of printed files
  • F.readline () #读一行
  • F.read () #读取所有内容, do not use large files, because the contents of the file will be read into memory, memory is not enough, the memory will be blown
  • 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, the memory will be blown
  • F.write (' Love certificate ') #写入内容
  • F.writelines ([' Love Certificate ', ' Stefanie Sun ']) #将一个列表写入文件中
  • F.tell () #获取当前文件的指针指向
  • F.seek (0) #把当前文件指针指向哪
  • F.fulsh () #写入文件后 to write data to disk immediately from memory
  • F.truncate () #清空文件内容, starting from the position pointed to by the pointer
  • F.close () #关闭文件

Large files, the efficient way to do this:

When working with large files, when using the Read () and ReadLines () methods, all the contents of the file are read into memory first, so that more memory will be very card. The workaround is to read a row of operations, so that the read content is freed from memory.

    • f = open (' file.txt ')
    • For line in F:
    • Print (line)

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:

    • 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,open (' File_bak ', ' W ') as FW: #这个是多文件的操作, open two files, FR is read FILE.TXT,FW is a new File_bak file
    • For lines in fr: #循环file. txt for each row
    • Fw.write (line) #写到file_bak文件中

To modify a file:

Modify the file, there are two ways, one is to read all the contents of the file into memory, and then empty the original file content, re-write the new content, the second is to write the modified file content into a new file

    • First Kind
    • With open (' file.txt ', ' r+ ') as fr:
    • res = Fr.read ()
    • New_res = Res.replace (' Me ', ' me ')
    • Fr.write (New_res)
    • The second Kind
    • With open (' file.txt ') as Fr,with open (' File_new ', ' W ') as FW: #这个是多文件的操作, open two files, FR is read FILE.TXT,FW is a new File_bak file
    • For lines in fr: #循环file. txt for each row
    • New_line = Line.replace (' Me ', ' me ')
    • fw.write (new_line) #写到file_bak文件中

Basic operations for Python Learning---files

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.