Python file operations

Source: Internet
Author: User

1. Open File

obj = file (' path and filename ', ' Open with ')

obj = open (' File path and filename ', ' Open with ') recommends using open

#r, W,a

obj = open (' log ')

Obj.tell () #取到指针的位置

Obj.seek (0) #跳到指定位置

PS: for open mode + sign only r+ is read-write, w+,a+ is equivalent to w a

Open mode RB opens in read-only binary binary

R+u to convert \ r \ n automatically to \ n

Open the file, the operation of the file will often forget to close the file, you can use with

With open (' Log ', ' R ') as obj:

The file is closed automatically once the code executed with it executes.

With support for opening multiple files after python2.7, rollback is also supported (e.g., configuration file modification error)

old.conf = R, new.conf W

With open (' Log1 ', ' R ') as Obj1,open (' log2 ', ' W ') as Obj2

For Lin Obj1:

New_line = Line.replace (' 10.6.7.8 ', ' 10.6.7.9 ')

Obj2.write (New_line)

The contents of the old configuration file can be read to the new configuration file, after the completion of the directory will generate two configuration files, check the new configuration file if there is no error

The new configuration file will work, and if it is wrong, use the original configuration file

Homework

Gracefully Modify the configuration file

Find

Add to

Delete

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.