Python ---- file operations,

Source: Internet
Author: User

Python ---- file operations,

File Operations:
Read:
F = open ("file1", "r", encoding = "UTF-8 ")

F. read () ------ read all in string format.
F. readline () -- read the first row
F. readline () -- read the second row
F. readlines ()-read all and form a list.
Print (f. tell () -- view the cursor position after the read operation is complete
F. seek (0) ----- place the cursor at the starting position
F. close ()
When reading files through the above method, the content will be stored in the memory. If large files are used, this will not work. Therefore, we do not recommend this in the future.
After a file is opened, one row is read, and only one row of content is stored in the memory. The method is as follows:
For I in f: --- at this time, f is an iterator?
Print (I)
F. close ()


Write:
F = open ("file2", "w", encoding = "UTF-8") --- 'W' is created. If yes, It is overwritten.
F. write ("Dummies ")
F. close ()

F = open ("file2", "r +", encoding = "UTF-8") --- read/write mode, which can be read first and then written, but can only be written to the end

F = open ("file2", "w +", encoding = "UTF-8") --- create and re-read mode, even if the cursor is placed in the front area, and then write again, it can only be at the end.

F = open ("file1", "rb") --- open in binary and read mode
F = open ("file1", "wb") --- open in binary or write mode
F = open ("file1", "AB") --- open in binary or append Mode

F. flush () ----- write to the hard disk only after memory accumulation reaches a certain level by default. This Command forces the disk to be refreshed and written to the hard disk, which plays a real-time role.
Progress bar:
Import sys, time
For I in range (20 ):
Sys. stdout. write ("#")
Sys. stdout. flush ()
Time. sleep (1)

Modify:
Idea: Read a row from one row of the old file, read a row, and judge a row. Write a row to the new file.
F = open ("file1", "r", encoding = "UTF-8 ")
F_new = open ("file1.bak", "w", encoding = "UTF-8 ")
For line in f:
If "Hahahaha" in line:
Line = line. replace ("Hahahaha", "I am so good ")
F_new.write (line)
F. close ()
F_new.close ()
OS. remove ("info.txt") --------- Delete source file
OS. rename ("info_new.txt", "info.txt") ------ rename it back

To avoid forgetting to close a file, you can use the following method and open multiple files:
With open ("file1", "r", encoding = "UTF-8") as f ,\
Open ("file1.bak", "r", encoding = "UTF-8") as f2:
Print (f. readlines ())
Print ("-" * 20)
Print (f2.readlines ())

 

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.