python-file Operations

Source: Internet
Author: User

Open (File, mode= ' R ', Buffering=-1, Encoding=none, Errors=none, Newline=none, Closefd=true)

Opening a file and returning a stream also allows you to read and write Stringio (text) or Bytesio (binary) as a file

FileName
-
File name or URL.

Mode
-
Open mode, which defaults to read-only ' R '.

    • ' R ', read-only (default).
    • ' W ', write only, and empty the contents of the file.
    • ' A ', append the content to the end of the file.
    • ' B ', binary.
    • ' t ', text (default).
    • ' + ', read and write.

Buffering
-
Optionally, set the caching method.

    • 0, disable caching (useful only in binary mode).
    • 1, row cache (useful only in text mode).
    • -1, fixed-size block cache.

Encoding
-
Optional, character encoding, useful in text mode.

Errors
-
Optional, how to handle encoding errors.

    • ' Strict ', there is a coding error when the ValueError exception is thrown (same as the default value of None).
    • ' Ignore ', ignoring errors and potentially causing data loss.

NewLine
-
Optional, control newlines. Can be none, ' ', ' \ n ', ' \ R ', ' \ r \ n '.

Closefd
-
Optionally, if CLOSEFD is false, the underlying file descriptor will remain open when the file is closed. This parameter must be true when a file name is provided.

return value

Returns the file object, if it is a text file, that returns Textiowrapper. If it is a binary file, it returns BufferedReader when read, and returns BufferedWriter when it is written.

1. File Operation method:

F.read Read all the contents of the file

F.read Read all the contents of the file

F.readline reading a line of content

F.readlines reads each line of content and returns a list

F.close () Close file

F.seek () Moves the pointer to the specified position

F.tell () Gets the current pointer position

F.flush () writes the cache to the hard disk

F.mode () show file Open format

F.truncate () f.truncate (10) Intercept file 0-10 characters, out of section Delete

F.writelines () passes in a list and writes each element of the list to a file

F.xreadlines () iterates through the file, one at a time, and one line at a time. High efficiency in handling large files

2.1 F.read Read all the contents of the file

2.2 F.readline reads a line of content

2.3 F.readlines reads each line of content, returns a list

2.4 F.seek () Moves the pointer to the specified position

2.5 F.tell () Gets the current pointer position

2.6 F.truncate ()

F.truncate (10) Intercept file 0-10 characters, out of section Delete

2.7 F.writelines () passes in a list and writes each element of the list to a file

2.8 f.xreadlines () python3.x deprecated

Iterates through the file, one at a time, and one line at a time. High efficiency in handling large files

3.for Loop File Object

3.1with opening multiple file operations

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.