python打開內置函數:模式a,a +,w,w +和r +之間的區別?

在python內置的open函數中,模式w , a , w+ , a+和r+的確切區別是什麼?

python打開內置函數:模式a,a +,w,w +和r +之間的區別?

特別地,文檔暗示所有這些都將允許寫入文件,並說它打開文件專門用於“追加”,“寫入”和“更新”,但未定義這些術語的含義。

1.打開模式與C標準庫函數fopen()完全相同。

BSD fopen聯機幫助頁對它們的定義如下

<code>The argument mode points to a string beginning 

with

one

of

the

following

sequences (Additional

characters

may follow these sequences.):

``

r

''

Open

text

file

for

reading. The stream

is

positioned

at

the

beginning

of

the file.

``

r+

''

Open

for

reading

and

writing. The stream

is

positioned

at

the

beginning

of

the file.

``

w

''

Truncate

file

to

zero

length

or

create

text

file

for

writing. The stream

is

positioned

at

the

beginning

of

the file.

``

w+

''

Open

for

reading

and

writing. The

file

is

created

if

it does

not

exist, otherwise it

is

truncated. The stream

is

positioned

at

the

beginning

of

the file.

``

a

''

Open

for

writing. The

file

is

created

if

it does

not

exist. The stream

is

positioned

at

the

end

of

the file. Subsequent writes

to

the

file

will

always

end

up

at

the

then

current

end

of

file

, irrespective

of

any

intervening fseek(

3

)

or

similar.

``

a+

''

Open

for

reading

and

writing. The

file

is

created

if

it does

not

exist. The stream

is

positioned

at

the

end

of

the file. Subse- quent writes

to

the

file

will

always

end

up

at

the

then

current

end

of

file

, irrespective

of

any

intervening fseek(

3

)

or

similar./<code>

2.這些選項與C標準庫中的fopen函數相同:

1.w截斷文件,覆蓋已存在的文件

2.文件a追加,添加到已經存在的任何內容上

3.w+打開以進行讀取和寫入,截斷文件,但也允許您回讀已寫入文件的內容

4.a+打開以進行追加和讀取,使您既可以追加到文件,也可以讀取其內容

3.我碰巧試圖弄清楚為什麼要使用模式“ w +”與“ w”。

最後,我只是做了一些測試。 我看不到'w +'模式有什麼用,因為在兩種情況下,文件都是從頭開始被截斷的。 但是,有了“ w +”,您可以在寫完後通過回頭閱讀。 如果您嘗試使用“ w”進行任何讀取,都會引發IOError。 在模式“ w +”下不使用seek進行讀取不會產生任何結果,因為文件指針將位於您寫入的位置之後。


4.我注意到,我不時需要重新打開Goog​​le,只是為了構想兩種模式之間的主要區別是什麼。 因此,我認為下一次閱讀圖會更快。 也許其他人也會發現它也有幫助。


5.相同信息,只是表格形式

<code>           | r   r+   w   w+   a   a+
 
read              | +   +        +        +
write             |     +    +   +    +   +
write after seek  |     +    +   +

create

| + + + +

truncate

| + +

position

at

start

| + + + +

position

at

end

| + +/<code>

意義在哪裡:(為避免任何誤解

1.讀取-允許從文件讀取

2.寫-允許寫入文件

3.create-如果尚不存在則創建文件

4.截斷-在打開文件期間將其清空(刪除了文件的所有內容)

5.開始位置-打開文件後,初始位置設置為文件的開始

6.末尾位置-打開文件後,將初始位置設置為文件末尾

注意: a和a+始終附加在文件末尾-忽略任何seek運動。

順便說一句。 至少在我的win7 / python2.7上,對於以a+模式打開的新文件而言,有趣的行為是:

<code>

write

(

'aa'

);

seek

(

0

,

0

);

read

(

1

);

write

(

'b'

)

write

(

'aa'

);

seek

(

0

,

0

);

read

(

1

);

write

(

'b'

) -第二次

write

被忽略

write

(

'aa'

);

seek

(

0

,

0

);

read

(

2

);

write

(

'b'

)

write

(

'aa'

);

seek

(

0

,

0

);

read

(

2

);

write

(

'b'

) -第二次

write

引發IOError/<code>

最後

小編近幾年在學習Python!對於想學習Python的朋友們,我想說:很多人學了一個星期就放棄了,為什麼呢?其實沒有好的學習資料給你去學習,你們是很難堅持的,這是小編收集的Python入門學習資料!

python打開內置函數:模式a,a +,w,w +和r +之間的區別?


分享到:


相關文章: