Python file operation implementation code

Source: Internet
Author: User

Open (filename, mode, buffer)
The first parameter is the name of the file to be opened, which is required. The second parameter is the open mode, which is optional. The third parameter is the buffer zone and the third parameter is optional. By default, files are opened in Read mode. This function returns a stream type object.
There are several modes:
R: Read mode (default)
W: Write mode
A: append write mode
B: Binary Mode
T: text mode (default)
+: Update existing hard disk files (read and write modes)
U: Universal line feed mode)
If the opened file does not exist or there are other problems, an IOError exception occurs.
Common file object attributes:
Mode: file opening mode
Name: name of the opened file
Closed: whether the file is closed
Common file object methods include:
Tell (): Get the current location in the current file. The start value is 0.
Seek (position, mode): Move in the current file. The first parameter indicates the distance to be moved, and the second parameter indicates the mode: 0 indicates the absolute position to move, relative to the file header; 1 indicates the relative position to move, as far as the current position is concerned; 2 indicates the position relative to the end of the file.
Read (max_byte_num): read bytes from the file. Max_byte_number is an optional parameter, indicating the maximum number of bytes read. If this parameter is not selected, the system reads data to the end of the file by default. After reading, the current location changes, that is, the number of bytes read is increased.
Readline (): one row that reads a file at a time.
Write (content): write data to a file. Content is the content to be written.
Close (): close the file
An example of file read/write: Copy codeThe Code is as follows: try:
F = open ('d:/hello_python.txt ', 'w ')
F. write ('Hello my friend python! ')
Handle t IOError:
Print ('ioerror ')
Finally:
F. close ()
Try:
F = open ('d: \ hello_python.txt ', 'R ')
Print (f. read ())
F. close ()
F. tell ()
Failed t ValueError as ioerror:
Print ('file alread closed {0} '. format (type (ioerror )))
Finally:
Print ('operation ')

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.