Python file read and write mechanism

Source: Internet
Author: User
Tags readline

Python provides the necessary functions and methods for basic file operation by default

How to open the file:

Open (Name[,mode[buf]])

Name: File path

Mode: Open mode

BUF: Buffer buffering size

File read mode:

Read ([size]): Read file (read size byte, read all by default)

ReadLine ([size]): reads a row

ReadLine ([size]): Read buffer buf (IO. Default_set_buffer), returns the list of each row

ITER: Using an iterator to traverse the Read file F.open (name); iter_f = iter (f); Using the for line in Iter_f Loop iterator

File Write Method:

Write (str): Writes a string to a file

Writelines (sequence_of_strings): Writes multiple lines to a file, with arguments as objects that can be iterated

When write (str) is called, the Python interpreter calls the system call to write the contents to disk, but the Linux kernel has a file caching mechanism, so caches the kernel buffers, and when close () or flush () is called, the content is actually written to the file

or write -in data volume greater than or equal to write cache, write cache will also be synchronized to disk

Purpose of closing the file

1: Write cache sync to disk

The number of open files per process in the 2:linux system is limited

3: If the number of open files to the system limit, the open file will fail

Python file pointer operation:

Seek (Offset[,whence]) move the file pointer

Offset: Offsets, can be a negative number

Whence: Offset relative position

How the Python file pointer is positioned:

Os. Seek_set start position relative to the file 0

Os. Seek_cur relative to the current position of the file 1

Os. Seek_end position relative to end of file 2

Python file properties:

File.fileno (): File descriptor;

File.mode: File open permission;

File.encoding: File encoding method;

File.closed: Whether the file is closed;

Python Standard Files:

Standard input file: Sys.stdin; Read-only descriptor is 0

Standard output file: sys.stdout; Write-only descriptor is 1

Standard error file: Sys.stderr; Write-only descriptor is 2

Python command-line arguments:

The SYS module provides the SYS.ARGV property, which allows you to get command-line arguments. SYS.ARGV is a string sequence that holds the command-line arguments, where sys.argv[0] is the file name and 1~n is the true argument

Python File Encoding method

The default encoding format for Python files is ASCII format, to be written in Chinese you can convert the encoding format

1. A = Unicode.encode (U ' hello ', ' utf-8 ') conversion, a Chinese character occupies 3 bytes in ASCII code and 2 bytes in Unicode.

2. Create a file in utf-8 format directly. Create the specified encoding format file using the methods provided by the codecs module:

Codecs.open (fname, mode, encoding, errors, buffering): Open a file using the specified encoding format

3. You can also create a file in the specified encoding format using the system-provided open () function:

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

Simple Linux File system


Here is the process for Python action files


Reading these, is not the Python file mechanism has a certain understanding of the ^^!


Andy
Source: http://www.cnblogs.com/onepiece-andy/
The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the author's consent must be in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Python file read and write mechanism

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.