python--Files and Streams

Source: Internet
Author: User

First, open the file:

Open (name[, mode[, Buffering]): name refers to the file name, mode refers to the modal parameter, and buffering refers to Buffering.

1, if not found will throw an exception.

2. File modes: Mode

' R ': Read mode

' W ': Write mode

' A ': Append mode

' B ': binary mode, such as a sound clip or image, requires the use of binary mode.

' + ': Read/write mode.

3, Buffer: buffering.

Control the buffer of the file, if the parameter is 0 or false,i/o input output is unbuffered (all read and write directly to the hard disk), if 1 or true,i/o is buffered (meaning to use memory instead of hard disk, to make the program faster, Update to hard disk only if flush or close is used).

Numbers greater than 1 represent the size of the buffer (in bytes), 1 or any negative number represents the use of the default buffer size.

Ii. Basic Documentation methods:

1. Read and write:

2. Pipeline output: The function of the Linux shell pipe character, which connects the standard output of a command with the standard input of the next command.

Cat Somefile.txt | Python somescript.py | The sort:somescript.py reads the data from its Sys.stdin (the Cat Somefile.txt writes) and writes the results to its sys.stdout.

3. Random Access:

Seek (offset[, whence]): Moves the current position (where it is read and written) to the location defined by offset and whence. Offset indicates that the offsets must be non-negative. Whence: The default of 0 indicates that the offset is calculated from the beginning of the file. The whence is set to 1 (moves relative to the current position, at which point offset can be a negative number) or 2 (as opposed to the end of the file).

Tell method: Returns the location of the current file.

4. Read-write line: File.readline reads a single line (starting from the current position until a newline character appears, and also reads a newline character)

5. Close the file:

1. You can use the try/finally statement:

Try

#write data to your file

Finally

#file. Close ()

2. Use the WITH statement:

From __future__ import with statement

With open ("Somefile") as Somefile

Do Something (somefile)

6, the basic method of using the file:

7. Iterate over the contents of the file:

1. Use the while loop to process bytes:

2, by line operation:

3. Read all content:

Premise: The file is not very large.

Use read to iterate through each character:

To iterate a row using ReadLines:

4. Use Fileinput to implement lazy iteration:

5. File iterators: File objects are iterative and can be iterated by using them directly in the For loop.

python--Files and Streams

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.