Python knowledge about file operations

Source: Internet
Author: User
This article mainly introduces Python's collection of file operation knowledge, which is of great reference value, for more information about Python file operations, see the next article.

Open a file

Operation file

1. when opening a file, you must specify the file path and opening method.

Open mode:

R: Read-only
W: write only
A: append

"+" Indicates that a file can be read and written simultaneously.

R +: Read/Write
W +: write and read
A +: same as

U "indicates that \ r \ n can be automatically converted to \ n (used in the same mode as r or r +) during read)

RU
R + U

"B" indicates processing binary files (for example, sending and uploading ISO image files via FTP, which can be ignored in linux and must be noted when processing binary files in windows)

Rb
Wb
AB

F = open ('test. log', 'R + ', encoding = 'utf-') f. write ('sdhgrbfds in saf ') print (f. tell () # view the current pointer position, in the unit of characters f. seek () # specifies the current pointer position, in bytes print (f. read () f. truncate () # print (f. tell () f. close ()

II. common file operations

F = open ('data', 'r') # open in read-only mode (read-only by default)
F = open('f.txt ', encoding = 'Latin-1') # python3.0 Unicode file
String = f. read () # read the file into a string
String = f. read (N) # N bytes after reading the pointer
String = f. readline () # read the next row, including the end identifier of the row
Alist = f. readlines () # read the entire file to the string list
F. write () # write a string to a file
F. writelines () # write all strings in the list to a file
F. close () # manually close
F. flush () # fl the output buffer to the hard disk.
F. seek (N) # Move the file pointer to N, in bytes
For line in open ('data '):
Print (line) # The File iterator reads a row of the file
Open('f.txt ', 'r'). read () # read all at ance into string

3. store and parse python objects in files

X, y, z = 41,42, 43 s = 'spam' D = {'a': 1, 'B': 2} # Dictionary object L = ['A ', 'B', 'C'] # List f = open('f.txt ', 'w') f. write (s + '\ n') f. write ('% s, % s, % s \ n' % (x, y, z) f. write (str (D) f. write ('\ n'f.write(str(l00000000f.close(+print(open('f.txt '). read () # output file content # retrieve data from the file and determine its type ''' a = fi. readline () B = fi. readline () c = fi. readline () d = fi. readline () print (a, B, c, d, type (a), type (B), type (c), type (d )) ''' # retrieve data from the file and convert it to the type fi = open('f.txt 'before storage ') A = fi. readline (). rstrip () # rstrip () removes the line feed print (a, type (a) B = fi. readline (). rstrip (). split (',') # string's split () method. write a separator in brackets to split the string into a list. Print (B, type (B) c = fi. readline () C = eval (c) # Call the built-in function eval () to convert the string into executable python code. Print (C, type (C), type (c) d = fi. readline () D = eval (d) print (D, type (D), type (d ))

The above is a summary of Python's file operations knowledge. I hope it will help you!

For more Python file operations related articles, please follow the PHP Chinese network!

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.