Python file operations

Source: Internet
Author: User

Codecs module: Handle garbled problem during file operation.

F=codecs.open (' Fxh.txt ', ' r+ ') #打开文件

Open mode:

R, W, A, B, r+, w+ ....

Common methods:

Read () reads the contents of the file

Write () writes the content, must pass in the string

WriteLine () write content, must pass in a sequence

Next line after next () cursor

Seek () sets the position of the cursor

Tell () to see where the cursor is

Flush () flush to disk

Name File name

Closed whether the shutdown is off or true

To open a file using with: (a file that is opened with the with does not have to be closed manually)

Eg: Prints a specific line of the file.

With Codecs.open (' fxh.txt ', ' r+ ') as FD:
#text =fd.read ()
For Line,value in Enumerate (FD):
if line = = 2-1:
Print (value)
The above implementations can also be implemented using a single module:
Import Linecache
Test=linecache.getline (' Fxh.txt ', 2)
Print (test)

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.