File operations in Python

Source: Internet
Author: User

Basic IO functions in Python:

1) keyboard input function: Raw_input (string), does not handle the display, and returns.

Input (string), you can accept a Python expression as a return, and Python gets the result of the calculation inside

2) file Handle object: the creation of the document object must be obtained through the open () function,

File Object = open (file_name, Access_mode, buffering)

file_name, the path to the file that needs to be pointed to,

Access_mode,r,w,a ( plus , indicates read and write permissions,b is written in binary format)

          write operations, you can create files, but read operations and append operations must be based on the original file.

Buffering, if 0, indicates that the system does not cache the contents of the file as 1, indicating that the system caches the content, indicating the specific size of the cache.

properties and methods for file object :

Attribute: 1) Closed, indicates whether the file has been closed;

2) mode, which indicates how the file was opened;

3) name, which indicates the path name of the file;

4)

Method: 1) Close () closes the file handle and flushes the information in the buffer to the file;

2) write (), put any string, write to the file, line end newline character and intact ; writelines, write one line or several lines, add a newline character ,

3) Read (count), reads a string of a certain size, does not specify, reads all; ReadLines, reads out a few lines , contains \ n;

4) Flush (), from memory, refresh the file;

5) Next (), returns the next line in the file;

6) Tell (), returns the position of the current file object ( not in the behavior unit );

7) Seek (offset, where), sets the location of the file object,0 means the file starts, 1 indicates the current position, and 2 indicates the end of the file;

The file's open and close must be symmetric, and if a problem occurs during file read and write, then close will not be executed, so in order to solve this problem,

Use try,,, finally to manipulate the file;

Try

f = open ('./... /.. /1.txt ', ' R ')

Print (F.read ())

....

Finally

If f:

F.close ()

This guarantees file close, but the writing is cumbersome, so Python introduces the use of with:

 With open ('./.. /1.txt ', ' R ') as F:

Print (F.read ())

In this compound statement, the file object is called, and then it is automatically close,

if you want to access the contents of each row, there are two ways to do it :

1) for F.readlines (): #ReadLines () returns a list

......

2) for line in F: #file object has a next function, which is iterable

....

File operations in Python

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.