How Python open opens a file

Source: Internet
Author: User

open/file Operations
F=open ('/tmp/hello ', ' W ')

#open (path + file name, read-write mode)

#读写模式: R read-only, r+ read/write, W New (overwrites the original file), a append, b binary. Common mode

such as: ' RB ', ' WB ', ' r+b ' and so on

The types of read-write modes are:

RU or Ua opens in read-only mode with universal line-feed support (PEP 278)
W opens in write mode,
A opens in Append mode (starting with EOF and creating a new file if necessary)
R+ Open in read-write mode
w+ Open in read/write mode (see W)
A + opens in read/write mode (see a)
RB opens in binary read mode
WB opens in binary write mode (see W)
AB opens in binary append mode (see a)
Rb+ opens in binary read/write mode (see r+)
Wb+ opens in binary read/write mode (see w+)
Ab+ opens in binary read/write mode (see A +)


Attention:

1, use ' W ', if the file exists, first to empty, and then (re) create,

2, using the ' a ' mode, all the data to be written to the file is appended to the end of the file, even if you use Seek () to point to the file somewhere else, if the file does not exist, it will be created automatically.



F.read ([size]) size unspecified returns the entire file if the file size >2 memory is problematic. F.read () returns "" (empty string) when the end of the file is read

File.readline () returns a row

File.readline ([size]) returns a list that contains a size row, and returns all rows if the size is unspecified

For F:print Line #通过迭代器访问

F.write ("hello\n") #如果要写入字符串以外的数据, first convert him to a string.

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

F.seek (offset, [start position])

Used to move the file pointer

Offset: unit: bit, can be negative

Starting position: 0-File header, default value; 1-current position; 2-End of file

F.close () Close 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 was 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

How Python open opens a file

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.