Concise Introduction to file operations in Python _python

Source: Internet
Author: User

Open File

The open function returns a file object, the basic syntax:

File_object = open (file_name, access_mode= ' R ' [, Buffering=-1])
File_name is a string containing the name of the file you want to open, which can be a relative or absolute path.
Optional variable Access_mode is also a string that represents the mode in which the file is opened. Typically, files are opened using the pattern ' R ', ' W ', or ' a ' mode, representing read, write, and append, respectively.
Another optional parameter buffering is used to indicate the buffering method used to access the file. where 0 indicates no buffering, 1 indicates that only one row of data is buffered, and any other value greater than 1 represents the buffer size using the given value. Do not supply this parameter or a given negative value represents the use of the system default buffering mechanism

File built-in methods

Read out

The read (size) method is used to read bytes directly into a string and read up to a given number of bytes. If size is not given, the file will be read out.
The ReadLine () method reads a row of open files, the same as read (), and it also has an optional size parameter, which defaults to-1, which represents the read to line terminator. If this argument is provided, incomplete rows are returned after the size word is exceeded.
ReadLines () It reads all (remaining) rows and returns them as a list of strings.

Write

Write () writes to the character.
Writelines () write line. Note here that you need to display a write line break.

Moving within files

Seek (offset) moves the current read/write position to the specified offset position. The current read-write position changes after each write and read operation.

File iterations

New method (more efficient)

Copy Code code as follows:

For Eachline in F

The old way:
Copy Code code as follows:

For Eachline in F.readline ():

Close File

Close () Closes the file to end access to it. You may lose buffer data when you write to a file without closing the file.

Buffer data to write to file

The flush () method immediately writes the data in the internal buffer directly to the file.

Intercepting files

The truncate () method intercepts the file to the current file pointer location or to the given size, in bytes.

Example explanation

Copy Code code as follows:

#!/usr/bin/python
#coding =utf-8

#以写方式打开一个名为welcome. txt file
f = open ("Welcome.txt", ' W ')

#将数据写入文件
F.writelines ("Welcome to here\n")
F.writelines ("Thank you\n")
F.writelines ("exit\n")

#刷新文件
F.flush ()

#关闭文件
F.close ()

#以读方式打开一个名为welcome. txt file
f = open ("Welcome.txt", ' R ')
Print F.read (8)

#重置读写位置, back to the beginning of the file
F.seek (0)
Print F.readline ()


Output
Copy Code code as follows:

Welcome
Welcome to Here

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.