Python learning _06_ file, IO

Source: Internet
Author: User

File Object

File manipulation in Python is similar to the C language, including some ways of buffering and offsetting.

The file object can be created by using the open (), file () two built-in methods, and the two methods are not different, similar to the fopen () in C language:

File_object = open (file_name, access_mode= ' R ', Buffering=-1)

Where Access_mode means open mode, R for Read only, W for write only, r+,w+ for read and write, a for append etc, buffering for buffering, negative for default buffering, 0 for unbuffered, 1 for row buffer, and greater than 1 for buffer size, In general, you can use the default buffer.

In Perl it is often used in the way of pipes, through open in, "GUNZIP-CD $gzfile |" or Die $!; Easy way to read from a compressed file, you can also implement pipeline operations in Python via Os.popen (), noting that the last layer of piping's | needs to be omitted because Popen () is read from the pipeline by default.

File input:

1. The read () method, specifies that a certain number of bytes are read into the string, and if no byte is specified or negative, all content is read;

2. ReadLine () method, reads a line of content, if the specified parameter size is less than one line length, returns only size bytes into the string;

3. The ReadLines () method reads all rows and returns a list of strings, the optional parameter represents the maximum number of bytes returned, but the buffer size still needs to be pooled;

Using iterators, and with the For Loop and ReadLine () methods, is the better way to enter:

For line in File_object.readline ():
? ? Print line?

File output:

1. Write () method, like file writing string

2. Writelines () method, writes a list of strings to a file, or an iterative object that returns a string

File offset:

1. The tell () method can return the current position in the file

2. Seek (offset, whence=0) method, move the pointer in the file, whence=0 means to move offset offsets from the beginning of the file, 1 means to move offset offsets from the current position, and 2 to move offset offsets from the end of a file

Built-in methods:

File.close (): Closes the file handle and writes the contents of the buffer to the file

File.fileno (): Returns the file descriptor;

File.flush (): Flush file buffers Now

Built-in properties:

File.name: filename, file.mode: File open mode, file.encoding: File Encoding method, file.closed: File is closed, etc.

Standard input and output error stream:

The SYS module can access the standard input and output error stream through Sys.stdin,sys.stdout,sys.syderr, and the built-in method Raw_input () typically receives input from Sys.stdin

The SYS module can also access the environment parameters through the SYS.ARGV property, Argv[0] as the script itself

File system

The OS module contains the main interfaces of the operating system functionality, including the related operations of the file:

OS.GETCWD (): Returns the current working directory, Os.listdir (): Lists the relevant files (including subdirectories) in the specified directory, chdir (): Change the current directory, Symlink (): Create Symbolic Link, remove (): Delete file, rename () : Renaming files

Path-related operations in the Os.path module:

Os.path.basename: Return file name, dirname (): Returns the directory name, Split (): Returns (DirName (), basename ()) tuple, Get*time (): Returns various events, GetSize () returns the file size, Exists (): Specifies whether the path exists, Isdir (): Whether the specified path exists and is a directory, Isfile (): Specifies whether the directory exists and is a file, and so on

Other related modules

BZ2, CSV, Gzip/zlib, Tarfile, ZipFile, etc.

Python learning _06_ file, IO

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.