Read and write Python files

Source: Internet
Author: User
Tags readable

File read and write: File open:

f = file (name, mode, [buffering])

Entry parameters:

    1. Name File name,
    2. mode option, String,
    3. Buffering whether buffered (0= not buffered, 1 = buffered, int number of >1 = buffer size)

return value:

    1. File Object

Mode options:

  • "R" opens in read-only file, exception occurs if the file does not exist
  • "W" is opened in writing, can only write files, if the file does not exist, create the file; if the file already exists, empty it before opening the file
  • "RB" is opened in binary read mode, only read files, if the file does not exist, an exception occurs
  • "WB" opened in binary write, can only write files, if the file does not exist, create the file , if the file already exists, first empty, then open the file
  • "RT" opens as a text read, can only read a file, and if the file does not exist, an exception occurs
  • "WT" is opened in text, can only write files, if the file does not exist, create the file , if the file already exists, first empty, then open the file
  • "Rb+" is opened in binary read, can read, write the file, if the file does not exist, an exception will occur
  • "wb+" is opened in binary write, can read, write the file, if the file does not exist, create the file , if the file already exists, first empty, then open the file

Common methods for file manipulation:
  • f = open (' file.txt ', ' r+ ', encoding= ' utf-8 ') #encoding参数可以指定文件的编码
  • F.readline () # reads a row, the type of the returned data is ' string '
  • F.readable () # To determine if a file is readable
  • F.writable () # To determine if a file is writable
  • F.encoding # Returns the encoding of the file
  • F.read () # reads all content, the type of the returned data is ' string '. 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 () #读取所有文件内容, the type of the returned data is ' list ', the element is the data for each row, and the large file is not used. because the contents of the file will be read into memory, memory is not enough, will be the memory burst
  • F.tell () # Gets a pointer to the current file pointing to the
  • F.seek (0) # pointing to the current file pointer
  • F.write (' Love certificate ') # Write a string to a file
  • F.writelines ([' 123 ', ' 456 ', ' 789 ']) # Write a list to a file
  • F.fulsh () #写入文件后 to write data to disk immediately from memory
  • F.truncate () #清空文件内容
  • F.close () # Close File


The efficient way to read files is by looping through the rows that read the file:
f = open (' file.txt ')
For line in F:
Print Line # lines are the contents of each row of files, and when you finish reading a line, you release a row of memory

using the WITH structure:
Files are closed after file operation, but closing files is often forgotten, and files are automatically closed after using the With method.
#打开一个文件, pay the handle to F for this file.
with open (' file.txt ', ' R ') as F:
For line in F:
print line

Read and write Python 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.