Python Basics-File operations

Source: Internet
Author: User

1. file operation Process

    • Open the file, get the file handle and assign a value to a variable
    • Manipulating a file with a handle
    • Close File

Grammar

Open (filename, mode)

Instance

2. File Open mode

    • R, read-only mode (default).
    • W, write-only mode. "unreadable; not exist; create; delete content;"
    • A, append mode. "Readable; not exist" create; "only append content;"

"+" means you can read and write a file at the same time

    • r+, can read and write files. "readable; writable; can be added"
    • w+, write and read
    • A +, with a

"U" means that the \ r \ n \ r \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ r or r+ mode can be converted automatically when reading

    • RU
    • R+u

"B" means processing binary files (e.g. FTP send upload ISO image file, Linux can be ignored, Windows process binary file should be labeled)

    • Rb
    • Wb
    • Ab

3. With statements

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

With open (' Log ', ' R ') as F: ...

This way, when the with code block finishes executing, the internal automatically shuts down and frees the file resource.

After Python 2.7, with also supports the management of multiple file contexts simultaneously, namely:

With open (' Log1 ') as Obj1, open (' log2 ') as Obj2:pass

4. Methods for File objects

F.read ()

To read the contents of a file, call F.read (size), which reads a certain number of data and then returns as a string or byte object.

Size is an optional parameter of the numeric type. When size is ignored or negative, all the contents of the file are read and returned.

F.readline ()

F.readline () reads a separate line from the file. The line break is ' \ n '. F.readline () If an empty string is returned, the last line has already been read.

F.readlines ()

F.readlines () returns all the rows contained in the file.

If you set the optional parameter Sizehint, the bytes of the specified length are read, and the bytes are split by row.

Another way is to iterate over a file object and then read each line:

This method is simple, but does not provide a good control. Because the processing mechanism of the two is different, it is best not to mix.

F.write ()

F.write (String) writes a string to the file, and then returns the number of characters written.

F.tell ()

F.tell () returns the current location of the file object, which is the number of bytes that are counted from the beginning of the file.

F.seek ()

If you want to change the current location of the file, you can use the F.seek (offset, from_what) function.

The value of From_what, if 0 indicates the beginning, if 1 represents the current position, and 2 indicates the end of the file, for example:

    • Seek (x,0): Moves x characters from the start position, which is the beginning of the first line character of the file
    • Seek (x,1): Indicates that x characters are moved backwards from the current position
    • Seek (-x,2): Represents moving the X-character forward from the end of a file

The From_what value defaults to 0, which is the beginning of the file. A complete example is given below:

f.close ()

In text files (those that do not have B in the Open file mode), they are only positioned relative to the starting position of the file.

When you have finished processing a file, call F.close () to close the file and release the system's resources, and throw an exception if you try to call the file again.

Resources:

1. http://www.cnblogs.com/alex3714/articles/5717620.html

2. http://www.w3cschool.cn/python3/python3-inputoutput.html

Python Basics-File operations

Related Article

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.