Python file method

Source: Internet
Author: User

Open File

With the open function, the syntax format is: Open (name[, mode[, Buffering]), name is open file name, mode is the way to open files, buffering control file buffer.

Mode is optional, the default is read-only mode, which is R, where the parameters used are

The value of the mode parameter in the Open function
Value Describe
R Read
W Write
A Append to end of file
B Binary mode, can be added to other modes
+ Read/write mode, can be added to other modes

Buffering optional, 0 means that the input and output is unbuffered, 1 is buffered, memory is used instead of the hard disk, the hard disk data is updated only when flush or close is used, or greater than 1 means the specified buffer size; Less than 0 means the default buffer size is used.

The class file object contains methods such as reading methods and writing methods, as well as locating cursors, closing class file objects, and so on.

Class file Object Methods
Read Read characters
ReadLine Read a row
ReadLines Reads all the rows of the entire file and returns them as a list
Write Write characters
Writelines Writes a list of strings to a file, but the program does not add new lines
Seek Positioning the cursor position,
Close Close File Object

Note that writing a file does not have a WriteLine method, because you can use the Write method.

Close File

Files that have been written should always be closed because Python may cache the data being written, and if the program crashes for some reason, the data will not be written to the file at all. To ensure that the file is closed, you should use the Try/finally statement and call the Close method in the finally clause.

f = open ('c:\file.txt')try:    f.write ('  Close the file')finally:    f.close ()

In addition, you can use the WITH statement to open a file and assign it to a variable. You can then write the data to the file. The file is automatically closed after the statement ends, even if the exception is the result of the end.

With open ('c:\file.txt') as F:    f.write ('close the file '

  

  

Manipulating files

Open the file to hit the class file object, you can use the class file object method to read and write operations.

For example, F = open (' File.xt ', ' R ') opens the file as read-only, and then str = F.read (n) indicates that n characters are read from the open file

Cycle through files per character

f = open (filename) while True:    = f.read (1)    if not break    print(char) f.close ()

Looping through files per line

f = open (filename) while True:    = f.readline ()    if  not Line :          Break    Print (line) f.close ()

If the file is not very large, the entire file can be read directly and then processed

f = open (filename) for in F.read ():    print(char) f.close ()
f = open (filename) for in f.readline ():    print(line) F.close ()

File iterator action File

In Python, file objects can be iterated, which means that they can be used directly in a for loop to iterate over them.

f = open (filename) for in F:    print(line) F.close () 

Sys.stdin can also be iterated, just like any other file object. If you want to iterate through all the rows in the standard input, you can use the following form

Import SYS  for inch Sys.stdin:     Print (line)

Python file method

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.