Python: Open/File Operations

Source: Internet
Author: User

Open/File Operations
F = open ('/tmp/hello', 'w ')

# Open (path + file name, read/write mode)

# Read/write mode: R read-only, R + read/write, W new (will overwrite the original file), a append, B binary file. Common Mode

For example, 'rb', 'wb ', 'r + B', etc.

The read/write modes include:

The Ru or UA is opened in Read mode and supports common line breaks (PEP 278)
W open in write mode,
A is opened in append mode (starting from EOF and creating a file if necessary)
R + Enabled in read/write mode
W + is enabled in read/write mode (see W)
A + is enabled in read/write mode (see)
RB is enabled in binary read mode.
WB is enabled in binary write mode (see W)
AB is enabled in binary append mode (see)
RB + is enabled in binary read/write mode (see R +)
WB + is enabled in binary read/write mode (see w +)
AB + is enabled in binary read/write mode (see A +)


Note:

1. Use 'W'. If the file exists, clear the file and create it again,

2. Use the 'A' mode to append all the data to be written to the file to the end of the file, even if you use seek () to point to other parts of the file, if the file does not exist, will be created automatically.

F. if the read ([size]) size is not specified, the entire file is returned. If the file size is greater than twice the memory size, the problem occurs. f. read () returns "" (Null String) when reading the end of a file)

File. Readline () returns a row

File. Readline ([size]) returns the list of rows containing size. If the size is not specified, all rows are returned.

For line in F: print line # access through iterator

F. Write ("Hello \ n") # If you want to write data other than a string, convert it to a string first.

F. Tell () returns an integer indicating the position of the pointer to the current file (that is, the number of bits to the file header ).

F. Seek (offset, [start position])

Used to move file pointers

Offset: Unit: Bit, positive or negative

Start position: 0-file header, default value; 1-Current position; 2-file end

F. Close () close the file

Code:

#! /Usr/bin/ENV Python
# Filename: using_file.py

Poem = ''' \ programming is funwhen the work is doneif you wanna make your work also fun: Use python! '''
F=file('poem.txt ', 'w') # Open for 'W' riting
F. Write (POEM) # write text to file
F. Close () # close the file
F=file('poem.txt ')

# If no mode is specified, 'R' EAD mode is assumed by default
While true:
Line = f. Readline ()
If Len (line) = 0: # Zero Length indicates EOF
Break
Print line,
# Notice comma to avoid automatic newline added by Python
F. Close ()
# Close the file

From: http://hi.baidu.com/zzfxz/blog/item/1c4d73cb4aa2c814bf09e613.html

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.