Python file operations

Source: Internet
Author: User

Write a little every day, one day I can become more salty salted

I. File reading and writing

Examples are as follows:

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 " "4 Read the file5 " "6 defread_file (filename):7     #Open the file as read-only, the file must exist, or throw an exception8F=open (filename,'R')9     #Read File contentsTenContent =F.read () One     Printcontent A     #Close File - f.close () -  the " " - Write a file - " " - defwrite_file (filename): +     #Open the file as written, and the file does not exist, re-create -f = open (filename,'W') +     #Write Content AF.write ('Hello World!!! \ n') at     #Close File - f.close () -  - if  __name__=="__main__": -Read_file ('./a.txt') -Write_file ('./b.txt')

Some of the modes for Open are as follows:

W is opened in writing, the file does not exist, it is created with overwrite write operation a in append mode (starting with EOF, creating a new file if necessary) r+ read-write mode open W+ read-write mode open (see W) a+ read-write mode Open (see a) RB Open WB in binary read mode opens in binary write mode (see W) AB opens in binary append mode (see a) RB+ opens in binary reading mode (see r+ ) WB+ opens in binary read/write mode (see w+< c8>) AB+ opens in binary read/write mode (see A +)

Some functions and methods of file operation

Name Role Note
Read (n) Read files by bytes (bytes) N Optional, do not fill the default read all
ReadLine () Read a row Reads again before closing, and then reads the next line
ReadLines () Read all Rows Returns the result as a list
Write (obj) Writing objects to a file General Write As String
Writelines (seq) Write a tuple to a file No newline writes, only data in tuples is written to
Close () Close File An exception will be thrown when the file is closed
Flush () Writes buffer data to the hard disk Data can be written to a file before the close file
Fileno () Returns the file label for a long integer type
Tell () Returns the location of the file's current operation, with the origin of the file header
Seek (Offest) Move the file action tag to the specified offest location

Files that are opened in a a+,a manner, each time a write operation is marked

Move to the end of the file and seek cannot take effect

Next () Move a file action tag to the next line
Turncate (N) Intercepts a file to a specified size Write permission required

  

Python file operations

Related Article

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.