Python study Note 6-file and input/output,

Source: Internet
Author: User

Python study Note 6-file and input/output,

6.1 file object

All Python operations on files are based on operations on file objects. From the creation of file objects. Open () [file ()] provides an interface for initializing input and output. When open () is opened successfully, a file object is returned.

Syntax of the open () method:

File_object = open (filename, access_mode, buffering)

Filename indicates the string of the file name to be opened. It can be a relative or absolute path.

Access_mode, indicating the open mode. Common modes include 'R', 'w', and 'A', which respectively indicate the Read mode, write mode, and append mode. With + indicates readable and writable, and with B indicates that the operation is performed in binary mode.

Buffering indicates the buffer mode, 0 indicates that the buffer is not cached, and 1 indicates that one row is buffered. a value greater than 1 is used as the buffer size.

6.2 file built-in method

6.2.1 Input

The read () method is used to read bytes into strings. The maximum number of specified bytes can be read. By default, the size parameter is not specified. The file is read to the end of the file.

The readline () method reads a row of open files. Then the entire line includes the row Terminator, Which is returned as a string.

The readlines () method reads all rows and returns them as a string list.

6.2.2 output

Write (), which is opposite to read () and readline (). It writes text data or binary data blocks to a file.

Writelines (), accepts a string list as a parameter and writes them to a file.

6.2.3 file Movement

The seek () method can be used to move the file pointer to different locations in the file. The default value 0 indicates the start of the file, 1 indicates the start of the file, and 2 indicates the end of the file.

Text (), which tells you the position of the current file pointer in the file, measured in bytes from the file header.

6.3 file Iteration

To iterate a file, we first think of reading the file to the list or other sequences before performing iteration.

F=open('demo.txt ', 'R ')

StringList = f. readlines ()

For eachstring in stringList:

...

But in fact, file objects can also be iterated, so there is:

F=open('demo.txt ', 'R ')

For eachline in f:

...

6.4

Close () ends access to a file by closing it. When a file is not referenced in programming, it must be closed. Do not ask why.

The fileno () method returns the opened file descriptor. It is often used for underlying operations.

6.5 File System

Most file access is implemented through the OS module. Frequently Used file operations include deleting or renaming a file, traversing the directory tree, and managing file access permissions. The OS. path module of another module can perform operations on directory names. Below are some common functions.

6.5.1 File Operations

Mkfifo () to create a named pipe.

Remove () to delete an object.

Rename (), rename the file.

Utime (), update the timestamp.

To generate all the file names under the directory tree.

2. Directory/folder

Chdir () to change the current working directory.

Chroot (), the Directory of the changed current process.

Listdir () to list the files in the specified directory.

Mkdir ()/makedirs (), create a directory/create a multi-level directory.

Rmdir ()/removedirs (), delete directories/delete multi-level directories.

3. Access/permission

Access () to verify the permission mode.

Chmod () to change the permission mode.

Umask () to set the default permission mode.

6.5.2 path operation function in OS. path

1. Separate

Basename (), remove the path name and return the file name.

Dirname (). Remove the file name and return the path name.

Split (), returns the (dirname (), basename () tuples.

2. Information

Getatime (), returns the last access time.

Getctime (), returns the file creation time.

Getmtime (), returns the object modification time.

Getsize (), returns the file size.

3. Query

Exists () to specify whether the path exists.

 

6.6 related modules

Base64: decodes binary and text strings.

Bianascii, which supports encoding and decoding of binary and ASCII strings.

Bz2: access the compressed file in bz2 format.

Fileinput, which provides a row iterator for multiple text files.

Shutil, which provides advanced file access.

Tarfile: reads and writes tar archive files and supports compressed files.

Zipfile: A Tool for reading and writing zip archives.

 

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.