Python file Operations

Source: Internet
Author: User

I. Use of file operations

1. Open a file and read the contents

Format: Open ("File name", modify the encoding type). Read ()

1 variable = open (" filename ", encoding="utf-8"). Read ()
2 print (variable) #读取文件中内容

Note: windows= default GBK python= default Utf-8

. Read (): Go to memory to read.

Note: Without specifying the encoding type, Windows will point to GBK by default.

. Read (): R = Read

2. Overwrite the content, or create a new file to write to.

Format: Open ("File name", "W", modify the Encoding type): W is the creation of a new file that exists directly within the file.

1 variable = open (" filename ",' W', encoding="utf-8  ",")     # file handle, Memory object 2 variable. Write (" I love Beijing Tian ' an door, \ n" )                  # Overwrite file contents   

Note: Overwrite the content in the file.

The default is "R" read value without modifying read and write

. Write (): w = Write

3. Append content to the source file

Format: Open ("File name", "a", modify the encoding type): A represents the appended meaning and is unreadable.

1 variable = open (" filename ",'a', encoding="utf-8  ",")     # file handle, Memory object 2 variable. Write (" I love Beijing Tian ' an door, \ n")                  # Overwrite file contents         

Note: Append the content to the file.

Variable. Close: Closes the file.

Two. File operation knowledge Points

Judge

File variable. Writable (): Determines whether the file is writable.

File variable. Readable (): Determines whether the file can be read.

File variable. Seekale: Determines whether the specified character position can be moved.

File variable. Isatty: Determine if the end device is open.

File variable. Closed (): Determines whether the file is open.

Function

File variable. Tell (): Returns the current position value by the number of characters. Some terminals are non-removable.

File variable. Seek (Specified number): Returns to the specified location in the file.

File variable. ReadLines (): Converts to a list, dividing elements by rows.

File variable. Strip (): Remove spaces and carriage returns.

File variable. Truncate (): Specifies that the range is truncated and is emptied without specifying it.

File variable. Flush (): Force flush, write to the hard disk after the flush is written.

File variable. Errors: Exception handling (not available)

File variable. Buffer (): Memory cache file.

File variable. Close (): Frees memory.

View

File variable. Encoding: Print file character encoding.

File variable. ReadLine: Prints the first line of the file.

File variable. Name: Prints the file name.

File variable. Fileno: Print an excuse IO number (not available)

Auto Close

Format: With open ("File name", "Read and write", "Escape Encoding") as variable.

Python file Operations

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.