Python file operations

Source: Internet
Author: User

File Operation Flow


File Operation Flow

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

2. Manipulating files with a handle

3. Close the file


The most basic open, can only read, can not operate.

data = open ("File", encoding= "Utf-8"). Read () print (data) print (type data) #字符串格
f = Open ("File", "R", encoding= "utf-8") file handle assigned to Fdata = F.read () data2 = F.read () when read here, Python will read from the last data the most After starting to read, that is, nothing. R Read file can not write, W create a file can not read, a append content does not overwrite the file, cannot read. F.read () read f.write () write a file Note: U means that you can automatically replace \ r \ n (with R or r+ mode) RU R+ub to process binaries (e.g. FTP send upload file, Li Nux can be ignored, Windows needs to be labeled when processing binary files)

Read the first five elements

Method One, F = open ("File", "R", encoding= "Utf-8") print (F.readline ()) print (F.readline ()) print (F.readline ()) Print (F.readli NE ()) Print (F.readline ()) F.close ()
Method Two, F = open ("File", "R", encoding= "Utf-8") for I in range (5): Print (F.readline ()) F.close ()

Line five does not print

F.readlines () is a list, F = open ("File", "R", encoding= "Utf-8") for I in F.readlines (): Print (I.strip ()) Each line is followed by a newline character, which is removed by strip;
 method One,   f = open ("file", "R", encoding= "Utf-8")    for index,i in enumerate (F.readlines ()):       if index  == 4:          print ("------Split line--------")            continue      print ( I.strip ()) 
Method Two, F = open ("File", "R", encoding= "Utf-8") Count = 0 for I in F:f has become an iterator if count = = 4:p Rint ("------Split line-------") Count + = 1 Continue end this loop and proceed to the next loop print (I.strip ()) Count + = 1

File Method:

f = Open ("File", "R", encoding= "Utf-8")

View current pointer position

Print (F.tell ()) print (F.readline ()) print (F.tell ())

Pointer back to the beginning of the file

F.seek (0) print (F.tell ())

View File Encoding

Print (f.encoding)

Do exception handling f.errors


See if it's an end device

Print (F.isatty ())

To determine if the cursor can be moved, true otherwise false

Print (F.seekable ())

Determine if a file is readable

Print (F.readable ())

Determine if the file is writable

Print (F.writable ())


Force refresh; Write data to the hard disk in real time;

Print (F.flush ())

Flush Case Progress bar

Import Sys,time for I in range: Sys.stdout.write ("") for I in Range: Sys.stdout.write ("") Sys.stdout.flush () Time.sleep (0.1)


Determine if the file is closed

Print (f.closed)

Truncated

f = Open ("File", "a", encoding= "Utf-8") f.truncate () empty the file without parameters F.truncate (10) Truncate 10 bytes from the beginning of the file

File read/write mode open (read and append write)

f = Open ("File", "r+", encoding= "Utf-8") print (F.readline ()) F.write ("===============") print (F.readline ()) F.close ()

The file read-write mode opens (creates a new file to write and read, and then writes in Append.) )

f = Open ("File", "w+", encoding= "Utf-8") print (F.readline ()) F.write ("===============\n") f.write ("===============\n") ) print (F.seek) print (F.readline ()) F.write ("+++++++++++++++\n") print (F.seek ()) print (F.readline ()) F.close ()

File append read/write mode open (can append write and read)

f = Open ("File", "A +", encoding= "Utf-8") print (F.tell ()) print (F.readline ()) F.write ("===============\n") f.write ("= = = ============\n ") print (F.seek) print (F.readline ()) F.write (" +++++++++++++++\n ") print (F.seek) print ( F.readline ()) F.close ()

File binary encoding method read; Apply to network transmission

f = Open ("File", "RB") print (F.readline ())

File binary encoding method write; apply to network transmission

f = Open ("File", "WB") Print (F.write ("Hhhhhhh". Encode ())) default is Utf-8 mode F.close ()

File binary encoding method append; apply to network transmission

f = Open ("File", "AB") Print (F.write ("Hhhhhhh". Encode ())) default is Utf-8 mode F.close ()

File modification

1. Load all into memory and re-write to file

2. After modification, write in another file

f = Open ("File", "R", encoding= "Utf-8") F2 = Open ("File2", "W", encoding= "Utf-8") for I in F:if ' 2222 ' in i:i = I.re Place ("2222", "BBBB") #i. Replace Replace function f2.write (i) F.close () F2.close ()

File operations automatically close with

Features: Automatically close files.

To avoid forgetting to close a file after opening it, you can manage the context.

Example: With open ("File", "R") as F:print (F.readline ())


python2.7 can open multiple files at the same time.

With open ("File", "R") as F, open ("File2", "R") as F2:for I in F:print (I.strip ()) for i2 in F2:p Rint (I2.strip ())

(Official Python requirement: No more than 80 codes in one line of code;)





This article is from the "506554897" blog, please be sure to keep this source http://506554897.blog.51cto.com/2823970/1945833

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.