成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

Python基礎(chǔ)之(六)文件

wuyangnju / 2754人閱讀

摘要:它則是以行為單位返回字符串,也就是每次讀一行,依次循環(huán),如果不限定,直到最后一個(gè)返回的是空字符串,意味著到文件末尾了。

讀文件

在某個(gè)文件夾下面建立了一個(gè)文件,名曰:130.txt,并且在里面輸入了如下內(nèi)容:

learn python
http://qiwsir.github.io
qiwsir@gmail.com
f = open("123.txt") #打開(kāi)已經(jīng)存在的文件,此文件在當(dāng)前目錄,若在其他目錄使用絕對(duì)路徑
for line in f:
    print line, #Python 3: print(line, end="")

learn python
http://qiwsir.github.io
liuguoquan@gmail.com

緊接著做下面的操作:

>>> for line2 in f:     #在前面通過(guò)for循環(huán)讀取了文件內(nèi)容之后,再次讀取,
...     print line2         #然后打印,結(jié)果就什么也顯示,這是什么問(wèn)題?
...
>>>

這不是什么錯(cuò)誤,是因?yàn)榍耙淮我呀?jīng)讀取了文件內(nèi)容,并且到了文件的末尾了。再重復(fù)操作,就是從末尾開(kāi)始繼續(xù)讀了。當(dāng)然顯示不了什么東西,但是Python并不認(rèn)為這是錯(cuò)誤。

在這里,如果要再次讀取,那就從新f = open("130.txt")

創(chuàng)建文件
f = open("1234.txt","w") #創(chuàng)建文件
f.write("hello") #向文件寫(xiě)入內(nèi)容
f.close() #關(guān)閉文件流

創(chuàng)建文件,我們同樣是用open()這個(gè)函數(shù),但是多了個(gè)"w",這是在告訴Python用什么樣的模式打開(kāi)文件。也就是說(shuō),用open()操作文件,可以有不同的模式??聪卤恚?/p>

模式 描述
r 以讀方式打開(kāi)文件,可讀取文件信息。
w 以寫(xiě)方式打開(kāi)文件,可向文件寫(xiě)入信息。如文件存在,則清空該文件,再寫(xiě)入新內(nèi)容
a 以追加模式打開(kāi)文件(即一打開(kāi)文件,文件指針自動(dòng)移到文件末尾),如果文件不存在則創(chuàng)建
r+ 以讀寫(xiě)方式打開(kāi)文件,可對(duì)文件進(jìn)行讀和寫(xiě)操作。
w+ 消除文件內(nèi)容,然后以讀寫(xiě)方式打開(kāi)文件。
a+ 以讀寫(xiě)方式打開(kāi)文件,并把文件指針移到文件尾。
b 以二進(jìn)制模式打開(kāi)文件,而不是以文本模式。該模式只對(duì)Windows或Dos有效,類Unix的文件是用二進(jìn)制模式進(jìn)行操作的。

從表中不難看出,不同模式下打開(kāi)文件,可以進(jìn)行相關(guān)的讀寫(xiě)。那么,如果什么模式都不寫(xiě),像前面那樣呢?那樣就是默認(rèn)為r模式,只讀的方式打開(kāi)文件。

使用with

使用with實(shí)現(xiàn)安全的關(guān)閉文件,不用close操作了

>>> with open("130.txt","a") as f:
...     f.write("
This is about "with...as..."")
...
>>> with open("130.txt","r") as f:
...     print f.read()
...
learn python
http://qiwsir.github.io
qiwsir@gmail.com
hello

This is about "with...as..."
>>>
文件狀態(tài)
import os
file_stat = os.stat("123.txt")
print file_stat

posix.stat_result(st_mode=33188, st_ino=13575445, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=57,
st_atime=1470988267,
st_mtime=1470988267,
st_ctime=1470988267) #文件創(chuàng)建時(shí)間
read/readLine/readLines

read:如果指定了參數(shù)size,就按照該指定長(zhǎng)度從文件中讀取內(nèi)容,否則,就讀取全文。被讀出來(lái)的內(nèi)容,全部塞到一個(gè)字符串里面。這樣有好處,就是東西都到內(nèi)存里面了,隨時(shí)取用,比較快捷;“成也蕭何敗蕭何”,也是因?yàn)檫@點(diǎn),如果文件內(nèi)容太多了,內(nèi)存會(huì)吃不消的。文檔中已經(jīng)提醒注意在“non-blocking”模式下的問(wèn)題,關(guān)于這個(gè)問(wèn)題,不是本節(jié)的重點(diǎn),暫時(shí)不討論。

readline:那個(gè)可選參數(shù)size的含義同上。它則是以行為單位返回字符串,也就是每次讀一行,依次循環(huán),如果不限定size,直到最后一個(gè)返回的是空字符串,意味著到文件末尾了(EOF)。

readlines:size同上。它返回的是以行為單位的列表,即相當(dāng)于先執(zhí)行readline(),得到每一行,然后把這一行的字符串作為列表中的元素塞到一個(gè)列表中,最后將此列表返回。

有這樣一個(gè)文檔,you.md,其內(nèi)容和基本格式如下:

    You Raise Me Up
    When I am down and, oh my soul, so weary;
    When troubles come and my heart burdened be;
    Then, I am still and wait here in the silence,
    Until you come and sit awhile with me.
    You raise me up, so I can stand on mountains;
    You raise me up, to walk on stormy seas;
    I am strong, when I am on your shoulders;
    You raise me up: To more than I can be.

分別用上述三種函數(shù)讀取這個(gè)文件。

>>> f = open("you.md")
>>> content = f.read()
>>> content #結(jié)果為字符串
"You Raise Me Up
When I am down and, oh my soul, so weary;
When troubles come and my heart burdened be;
Then, I am still and wait here in the silence,
Until you come and sit awhile with me.
You raise me up, so I can stand on mountains;
You raise me up, to walk on stormy seas;
I am strong, when I am on your shoulders;
You raise me up: To more than I can be.
"
>>> f.close()
>>> print content
>>> f = open("you.md")
>>> f.readline()
"You Raise Me Up
"
>>> f.readline()
"When I am down and, oh my soul, so weary;
"
>>> f.readline()
"When troubles come and my heart burdened be;
"
>>> f.close()

f = open("you.md")
while True:
   line = f.readline()
   if not line:         #到EOF,返回空字符串,則終止循環(huán)
       break
   print line ,         #Python 3: print(line, end="")

f.close()                #別忘記關(guān)閉文件
>>> f = open("you.md")
>>> content = f.readlines()
>>> content #結(jié)果為列表
["You Raise Me Up
", "When I am down and, oh my soul, so weary;
", "When troubles come and my heart burdened be;
", "Then, I am still and wait here in the silence,
", "Until you come and sit awhile with me.
", "You raise me up, so I can stand on mountains;
", "You raise me up, to walk on stormy seas;
", "I am strong, when I am on your shoulders;
", "You raise me up: To more than I can be.
"]

>>> for line in content:
...     print line ,         #Python 3: print(line, end="")
>>> f.close
讀很大的文件fileinput

如果文件太大,就不能用read()或者readlines()一次性將全部?jī)?nèi)容讀入內(nèi)存,可以使用while循環(huán)和readline()來(lái)完成這個(gè)任務(wù)。

此外,還有一個(gè)方法:fileinput模塊

>>> import fileinput
>>> for line in fileinput.input("you.md"):
...     print line ,        #Python 3: print(line, end="")
...
You Raise Me Up
When I am down and, oh my soul, so weary;
When troubles come and my heart burdened be;
Then, I am still and wait here in the silence,
Until you come and sit awhile with me.
You raise me up, so I can stand on mountains;
You raise me up, to walk on stormy seas;
I am strong, when I am on your shoulders;
You raise me up: To more than I can be.

還有一種方法,更為常用:

  >>> for line in f:
    ...     print line ,      #Python 3:  print(line, end="")
    ...
    You Raise Me Up
    When I am down and, oh my soul, so weary;
    When troubles come and my heart burdened be;
    Then, I am still and wait here in the silence,
    Until you come and sit awhile with me.
    You raise me up, so I can stand on mountains;
    You raise me up, to walk on stormy seas;
    I am strong, when I am on your shoulders;
    You raise me up: To more than I can be.

之所以能夠如此,是因?yàn)槲募强傻膶?duì)象,直接用for來(lái)迭代即可。

seek

這個(gè)函數(shù)的功能是讓指針移動(dòng)。

>>> f = open("you.md")
>>> f.readline()
    "You Raise Me Up
"
    >>> f.readline()
    "When I am down and, oh my soul, so weary;
"

>>> f.seek(0)  #回到文件的最開(kāi)始位置
>>> f.readline()
    "You Raise Me Up
"

此時(shí)指針?biāo)诘奈恢?,還可以用`tell()`來(lái)顯示,如

>>> f.tell()
    17L

seek()還有別的參數(shù),具體如下:

seek(...)
seek(offset[, whence]) -> None. Move to new file position.

whence的值:

默認(rèn)值是0,表示從文件開(kāi)頭開(kāi)始計(jì)算指針偏移的量(簡(jiǎn)稱偏移量)。這是offset必須是大于等于0的整數(shù)。

是1時(shí),表示從當(dāng)前位置開(kāi)始計(jì)算偏移量。offset如果是負(fù)數(shù),表示從當(dāng)前位置向前移動(dòng),整數(shù)表示向后移動(dòng)。

是2時(shí),表示相對(duì)文件末尾移動(dòng)。

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/38206.html

相關(guān)文章

  • 基礎(chǔ)如何學(xué)爬蟲(chóng)技術(shù)

    摘要:楚江數(shù)據(jù)是專業(yè)的互聯(lián)網(wǎng)數(shù)據(jù)技術(shù)服務(wù),現(xiàn)整理出零基礎(chǔ)如何學(xué)爬蟲(chóng)技術(shù)以供學(xué)習(xí),。本文來(lái)源知乎作者路人甲鏈接楚江數(shù)據(jù)提供網(wǎng)站數(shù)據(jù)采集和爬蟲(chóng)軟件定制開(kāi)發(fā)服務(wù),服務(wù)范圍涵蓋社交網(wǎng)絡(luò)電子商務(wù)分類信息學(xué)術(shù)研究等。 楚江數(shù)據(jù)是專業(yè)的互聯(lián)網(wǎng)數(shù)據(jù)技術(shù)服務(wù),現(xiàn)整理出零基礎(chǔ)如何學(xué)爬蟲(chóng)技術(shù)以供學(xué)習(xí),http://www.chujiangdata.com。 第一:Python爬蟲(chóng)學(xué)習(xí)系列教程(來(lái)源于某博主:htt...

    KunMinX 評(píng)論0 收藏0
  • 首次公開(kāi),整理12年積累的博客收藏夾,零距離展示《收藏夾吃灰》系列博客

    摘要:時(shí)間永遠(yuǎn)都過(guò)得那么快,一晃從年注冊(cè),到現(xiàn)在已經(jīng)過(guò)去了年那些被我藏在收藏夾吃灰的文章,已經(jīng)太多了,是時(shí)候把他們整理一下了。那是因?yàn)槭詹貖A太亂,橡皮擦給設(shè)置私密了,不收拾不好看呀。 ...

    Harriet666 評(píng)論0 收藏0
  • Python爬蟲(chóng)學(xué)習(xí)路線

    摘要:以下這些項(xiàng)目,你拿來(lái)學(xué)習(xí)學(xué)習(xí)練練手。當(dāng)你每個(gè)步驟都能做到很優(yōu)秀的時(shí)候,你應(yīng)該考慮如何組合這四個(gè)步驟,使你的爬蟲(chóng)達(dá)到效率最高,也就是所謂的爬蟲(chóng)策略問(wèn)題,爬蟲(chóng)策略學(xué)習(xí)不是一朝一夕的事情,建議多看看一些比較優(yōu)秀的爬蟲(chóng)的設(shè)計(jì)方案,比如說(shuō)。 (一)如何學(xué)習(xí)Python 學(xué)習(xí)Python大致可以分為以下幾個(gè)階段: 1.剛上手的時(shí)候肯定是先過(guò)一遍Python最基本的知識(shí),比如說(shuō):變量、數(shù)據(jù)結(jié)構(gòu)、語(yǔ)法...

    liaoyg8023 評(píng)論0 收藏0
  • python綜合學(xué)習(xí)機(jī)器學(xué)習(xí)

    摘要:通過(guò)前面幾節(jié)的學(xué)習(xí),已經(jīng)奠定了通往學(xué)習(xí)的基礎(chǔ),從這節(jié)開(kāi)始,來(lái)學(xué)習(xí)機(jī)器學(xué)習(xí)。一什么是機(jī)器學(xué)習(xí)機(jī)器學(xué)習(xí)讓機(jī)器從數(shù)據(jù)中學(xué)習(xí),進(jìn)而得到一個(gè)更加符合現(xiàn)實(shí)規(guī)律的模型,通過(guò)對(duì)模型的使用使得機(jī)器比以往表現(xiàn)的更好,這就是機(jī)器學(xué)習(xí)。 通過(guò)前面幾節(jié)的學(xué)習(xí),已經(jīng)奠定了通往AI學(xué)習(xí)的基礎(chǔ),從這節(jié)開(kāi)始,來(lái)學(xué)習(xí)機(jī)器學(xué)習(xí)。 一、什么是機(jī)器學(xué)習(xí) 機(jī)器學(xué)習(xí)(MachineLearning):讓機(jī)器從數(shù)據(jù)中學(xué)習(xí),進(jìn)而得到一...

    JackJiang 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<