Python: File Operations Summary file basic operations

Source: Internet
Author: User

First, the operation process of the document

1. Open the file, get the file handle and assign a value to a variable

2. Manipulate the file through a handle

3. Close the file

Second, the opening and closing of the file

A, open--open function of the file

Syntax: Open (File[,mode[,buffering[,encoding[,errors[,newline[,closefd=true]]])

[Parameter description]:

file--file location + file name, need to add quotation marks [note]: If the location of the file is not specified, it is in the current folder by default

Open mode of the mode--file

buffering--can take 0, 1, greater than 1 integers or negative numbers

negative TD style= "Text-align:center" >
buffering value action
0 < Span style= "font-size:14px" > Access files without a deposit (buffer)
1 The line is stored when the file is accessed Buffer (used in text mode only)
integers greater than 1
The buffer size of the storage area when accessing the file is system default

encoding--indicates what encoding is used for the returned data, typically using Utf-8 or GBK

errors--value generally have strict, ignore, when taking strict, character encoding if there is a problem will be error, when the Ignore, the character encoding if there is a problem, the program will ignore, continue to execute the following program

newline--can be obtained with a value of none,\n,\r,\r\n; used to differentiate line breaks, but this parameter is valid only for text mode

closefd--can take true or FALSE, which is true by default

Value of CLOSEFD Role
True The file parameter passed in is the filename

The file parameter is passed to the file descriptor (the file descriptor is a non-negative integer, and a file descriptor is returned in the UNIX kernel's system, when a file is opened)

Python: File Operations Summary file basic 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: 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.