Python學習之文件(二)

文件中的read/readline/readlines函數

前面學習了關於如何讀取文件的操作,但是我們發現在Python的文件幫助文檔中,有很多函數,其中就有read/readline/readlines這樣三個函數,那麼爲什麼要學習這三個函數呢??
現在開始做詳細解釋:
read函數

help(file.read)
Help on method_descriptor:

read(…)
read([size]) -> read at most size bytes, returned as a string.

If the size argument is negative or omitted, read until EOF is reached.
Notice that when in non-blocking mode, less data than what was requested
may be returned, even if no size parameter was given.

(END)

解釋: read([size]) - >讀取大多數字節,以字符串形式返回。
如果指定了參數size,就按照該指定長度從文件中讀取內容,否則,就讀取全文。被讀取出來的全文,全部塞到一個字符串裏面。這樣的好處就是所有的東西都在內存裏面的,可以隨時存取。
readline函數

help(file.readline)
Help on method_descriptor:

readline(…)
readline([size]) -> next line from the file, as a string.

Retain newline.  A non-negative size argument limits the maximum
number of bytes to return (an incomplete line may be returned then).
Return an empty string at EOF.

(END)
解釋:可選參數都同read,它以行爲單位返回字符串,即每次讀取一行,依次循環,如果不指定size,直到最後返回的是一個空字符串。即到文件末尾。
readlines函數

help(file.readlines)
Help on method_descriptor:

readlines(…)
readlines([size]) -> list of strings, each a line from the file.

Call readline() repeatedly and return a list of the lines so read.
The optional size argument, if given, is an approximate bound on the
total number of bytes in the lines returned.

(END)
解釋:它返回的是以行爲單位的列表,即相當於先執行readline(),得到每一行,然後每一行中的字符串作爲列表中的元素塞進一個列表中,最後將此列表返回。

開始驗證以上的三個函數

所使用的文本都存放在/pythonFile/file/路徑下,文件的名稱爲rtest.txt,文件的內容如下:
我選用了這兩天爆炸與朋友圈的比特幣病毒,也就是蠕蟲病毒。且不管叫什麼,反正是不是聽着代碼大師是不是很帥。當然你也可以使用python進行網絡滲透,但是隻建議做有意義的事哦。比如發掘漏洞並將其反饋給相關產品機構,千萬不要做危害網絡安全的事兒。

[root@python file]# pwd
/pythonFile/file
[root@python file]# cat rtext.txt 
What Happened to My Computer?
Your important files are encrypted.
Many of your documents,photos,videos,
databases and others are no longer 
accesssible because they have been encrypted.
Maybe you are busy looking for
a wayto recover your files,
but do not waste your time.
Nobody can recover your files without our
decryption service.Can I Recover My files?
Sure,We guarantee that you can recover all
your files safely and easily,
But you have not so enouth time.
you can decrypt some of your files and free.
Try now by clicking <Decryt>.
But if you want to decrypt all
your files,you need to pay.
Your only have 3 days to submit
the payment.After that the
price will be doubled.
Also,if you don't be able to
recover your file forever.
We will have free events for user who 
are so poor that they couldn't pay in 6 months.
[root@python file]# 


readline
首先使用read函數來進行操作

>>> tread = open("/pythonFile/file/rtext.txt")
>>> line = tread.read()
>>> line
"What Happened to My Computer?\nYour important files are encrypted.\nMany of your documents,photos,videos,\ndatabases and others are no longer \naccesssible because they have been encrypted.\nMaybe you are busy looking for\na wayto recover your files,\nbut do not waste your time.\nNobody can recover your files without our\ndecryption service.Can I Recover My files?\nSure,We guarantee that you can recover all\nyour files safely and easily,\nBut you have not so enouth time.\nyou can decrypt some of your files and free.\nTry now by clicking <Decryt>.\nBut if you want to decrypt all\nyour files,you need to pay.\nYour only have 3 days to submit\nthe payment.After that the\nprice will be doubled.\nAlso,if you don't be able to\nrecover your file forever.\nWe will have free events for user who \nare so poor that they couldn't pay in 6 months.\n"
>>> print line
What Happened to My Computer?
Your important files are encrypted.
Many of your documents,photos,videos,
databases and others are no longer 
accesssible because they have been encrypted.
Maybe you are busy looking for
a wayto recover your files,
but do not waste your time.
Nobody can recover your files without our
decryption service.Can I Recover My files?
Sure,We guarantee that you can recover all
your files safely and easily,
But you have not so enouth time.
you can decrypt some of your files and free.
Try now by clicking <Decryt>.
But if you want to decrypt all
your files,you need to pay.
Your only have 3 days to submit
the payment.After that the
price will be doubled.
Also,if you don't be able to
recover your file forever.
We will have free events for user who 
are so poor that they couldn't pay in 6 months.
>>> tread.close()
>>> 

上述例子中,我們發現如果原文本中有換行,那麼read( )函數在讀取的時候就將換行符讀取出來。再使用print函數的時候“”\n“”就被自動轉義換行了。

使用readline( )函數讀取

>>> treadline = open("/pythonFile/file/rtext.txt")
>>> reline = treadline.readline()
>>> reline
'What Happened to My Computer?\n'
>>> reline = treadline.readline()
>>> reline
'Your important files are encrypted.\n'
>>> reline = treadline.readline()
>>> reline
'Many of your documents,photos,videos,\n'
>>> 

每次操作一次readline( )就讀取一行

這種讀取好像和迭代一樣,說通俗一點就是迭代。那麼,既然可以這樣理解,這個文件可以直接通過用循環的方式來寫。

[root@python file]# cat treadline.py 
#!/usr/bin/env python
#coding:utf-8

treadline = open("/pythonFile/file/rtext.txt")
#
#
#use while loop


while True:
    lineread = treadline.readline()
    if not lineread:               #if returns none string,end loop
        break
    else:
        print lineread ,
treadline.close()
[root@python file]# 

執行結果:

[root@python file]# python treadline.py 
What Happened to My Computer?
Your important files are encrypted.
Many of your documents,photos,videos,
databases and others are no longer 
accesssible because they have been encrypted.
Maybe you are busy looking for
a wayto recover your files,
but do not waste your time.
Nobody can recover your files without our
decryption service.Can I Recover My files?
Sure,We guarantee that you can recover all
your files safely and easily,
But you have not so enouth time.
you can decrypt some of your files and free.
Try now by clicking <Decryt>.
But if you want to decrypt all
your files,you need to pay.
Your only have 3 days to submit
the payment.After that the
price will be doubled.
Also,if you don't be able to
recover your file forever.
We will have free events for user who 
are so poor that they couldn't pay in 6 months.
[root@python file]# 

特別注意,每次對文件操作完畢,請務必關閉文件,因爲文件打開後,會佔用系統內存,而且誤調用會消耗系統的CPU資源,當然這只是針對於一些特別大的文件,小文件並不會直接的感受的到。
使用readlines( )函數讀取

>>> rlines = treadlines.readlines()
>>> rlines
['What Happened to My Computer?\n', 'Your important files are encrypted.\n', 'Many of your documents,photos,videos,\n', 'databases and others are no longer \n', 'accesssible because they have been encrypted.\n', 'Maybe you are busy looking for\n', 'a wayto recover your files,\n', 'but do not waste your time.\n', 'Nobody can recover your files without our\n', 'decryption service.Can I Recover My files?\n', 'Sure,We guarantee that you can recover all\n', 'your files safely and easily,\n', 'But you have not so enouth time.\n', 'you can decrypt some of your files and free.\n', 'Try now by clicking <Decryt>.\n', 'But if you want to decrypt all\n', 'your files,you need to pay.\n', 'Your only have 3 days to submit\n', 'the payment.After that the\n', 'price will be doubled.\n', "Also,if you don't be able to\n", 'recover your file forever.\n', 'We will have free events for user who \n', "are so poor that they couldn't pay in 6 months.\n"]
>>> treadlines = open("/pythonFile/file/rtext.txt")
>>> rlines = treadlines.readlines()
>>> for line in rlines:
...     print line,
... 
What Happened to My Computer?
Your important files are encrypted.
Many of your documents,photos,videos,
databases and others are no longer 
accesssible because they have been encrypted.
Maybe you are busy looking for
a wayto recover your files,
but do not waste your time.
Nobody can recover your files without our
decryption service.Can I Recover My files?
Sure,We guarantee that you can recover all
your files safely and easily,
But you have not so enouth time.
you can decrypt some of your files and free.
Try now by clicking <Decryt>.
But if you want to decrypt all
your files,you need to pay.
Your only have 3 days to submit
the payment.After that the
price will be doubled.
Also,if you don't be able to
recover your file forever.
We will have free events for user who 
are so poor that they couldn't pay in 6 months.
>>> treadlines.close() 

注意當中Print後面逗號的使用,如果沒有逗號的話,那麼將會有兩個換行輸出。

讀取大文件

read/readline/readlines只是用來讀取小文件,當然readline()也可以用while循環來讀取大文件。也可以用readlin( )函數來讀取大文件,但是Python爲了更好的解決讀取大文件不方便的問題,又引入了一個模塊兒,文件輸入模塊兒fileinput
先來了解一下fileinput

dir(fileinput)
[‘DEFAULT_BUFSIZE’, ‘FileInput’, ‘all‘, ‘builtins‘, ‘doc‘, ‘file‘, ‘name‘, ‘package‘, ‘_state’, ‘_test’, ‘close’, ‘filelineno’, ‘filename’, ‘fileno’, ‘hook_compressed’, ‘hook_encoded’, ‘input’, ‘isfirstline’, ‘isstdin’, ‘lineno’, ‘nextfile’, ‘os’, ‘sys’]

裏面包含一些屬性和模塊兒
來看一下Input的使用
help(fileinput.input)
Help on function input in module fileinput:

input(files=None, inplace=0, backup=”, bufsize=0, mode=’r’, openhook=None)
input([files[, inplace[, backup[, mode[, openhook]]]]])

Create an instance of the FileInput class. The instance will be used
as global state for the functions of this module, and is also returned
to use during iteration. The parameters to this function will be passed
along to the constructor of the FileInput class.

(END)

直接來測試。這個測試依然使用上面的rtext.txt文件

>>> import fileinput
>>> bigfile = fileinput.input("/pythonFile/file/rtext.txt")
>>> for text in bigfile:
...     print text,
... 
What Happened to My Computer?
Your important files are encrypted.
Many of your documents,photos,videos,
databases and others are no longer 
accesssible because they have been encrypted.
Maybe you are busy looking for
a wayto recover your files,
but do not waste your time.
Nobody can recover your files without our
decryption service.Can I Recover My files?
Sure,We guarantee that you can recover all
your files safely and easily,
But you have not so enouth time.
you can decrypt some of your files and free.
Try now by clicking <Decryt>.
But if you want to decrypt all
your files,you need to pay.
Your only have 3 days to submit
the payment.After that the
price will be doubled.
Also,if you don't be able to
recover your file forever.
We will have free events for user who 
are so poor that they couldn't pay in 6 months.
>>> bigfile.close()

移動文件中的指針seek( )函數

help(file.seek)
Help on method_descriptor:

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

Argument offset is a byte count.  Optional argument whence defaults to
0 (offset from start of file, offset should be >= 0); other values are 1
(move relative to current position, positive or negative), and 2 (move
relative to end of file, usually negative, although many platforms allow
seeking beyond the end of a file).  If the file is opened in text mode,
only offsets returned by tell() are legal.  Use of other offsets causes
undefined behavior.

:
解釋:參數offset是一個字節數。可選參數whence默認爲0
whence爲0: 偏移量從文件開頭開始,偏移量應該是大於等於0的。
whence爲1如果是負數的話,表示從當前位置向前移動,如果是正數,表示從當前位置向後移動。
whence爲2如果是正數的話,表示移動到文件末尾,通常使用負數,雖然許多平臺允許超過文件的末尾。
在文本模式下如果打開文件,只有使用tell( )返回的偏移量是合法的。使用其它的偏移量是沒有被定義的動作。
示例演示:

>>> see = open("/pythonFile/file/rtext.txt")
>>> seetext = see.readline()
>>> seetext
'What Happened to My Computer?\n'
>>> seetext = see.readline()
>>> seetext
'Your important files are encrypted.\n'
>>> see.seek(0)                         #在這裏使用了指針
>>> seetext
'Your important files are encrypted.\n'
>>> seetext = see.readline()
>>> seetext
'What Happened to My Computer?\n'
>>> see.close()

注意,指針seek( )裏面至少要有一個給定的值,如果沒有給定值,那麼將會拋異常。

>>> see.seek()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: seek() takes at least 1 argument (0 given)
>>> see.close()

繼續測試:

>>> see = open("/pythonFile/file/rtext.txt")
>>> seetext = see.readline()
>>> seetext
'What Happened to My Computer?\n'
>>> seetext = see.readline()
>>> seetext
'Your important files are encrypted.\n'
>>> see.seek(7)
>>> seetext
'Your important files are encrypted.\n'
>>> seetext = see.readline()
>>> seetext
'ppened to My Computer?\n'
>>> see.close()
>>> 

文件中的迭代

學習迭代前,先來看幾個概念:
循環: 指在條件滿足的情況下,反覆執行同一段代碼。
迭代:指按照某種順序訪問對象中的元素,直到滿足想要的結果爲止。
遍歷:指滿足條件的情況下,按照一定的規則訪問樹形結構中的每一個節點,而且每個節點只訪問一次。
遞歸:不斷重複調用自身的過程。

回顧一下前面的文件的屬性和函數

dir(file)
[‘class‘, ‘delattr‘, ‘doc‘, ‘enter‘, ‘exit‘, ‘format‘, ‘getattribute‘, ‘hash‘, ‘init‘, ‘iter‘, ‘new‘, ‘reduce‘, ‘reduce_ex‘, ‘repr‘, ‘setattr‘, ‘sizeof‘, ‘str‘, ‘subclasshook‘, ‘close’, ‘closed’, ‘encoding’, ‘errors’, ‘fileno’, ‘flush’, ‘isatty’, ‘mode’, ‘name’, ‘newlines’, ‘next’, ‘read’, ‘readinto’, ‘readline’, ‘readlines’, ‘seek’, ‘softspace’, ‘tell’, ‘truncate’, ‘write’, ‘writelines’, ‘xreadlines’]

裏面有一個iter,這個就是迭代。

文件中的迭代工具

迭代工具通常也被叫做迭代器,英文稱iterator
依然使用文本/pythonFile/file/rtext.txt這個文本。

>>> treadlines = open("/pythonFile/file/rtext.txt")
>>> trlines = treadlines.readlines()
>>> trlines
['What Happened to My Computer?\n', 'Your important files are encrypted.\n', 'Many of your documents,photos,videos,\n', 'databases and others are no longer \n', 'accesssible because they have been encrypted.\n', 'Maybe you are busy looking for\n', 'a wayto recover your files,\n', 'but do not waste your time.\n', 'Nobody can recover your files without our\n', 'decryption service.Can I Recover My files?\n', 'Sure,We guarantee that you can recover all\n', 'your files safely and easily,\n', 'But you have not so enouth time.\n', 'you can decrypt some of your files and free.\n', 'Try now by clicking <Decryt>.\n', 'But if you want to decrypt all\n', 'your files,you need to pay.\n', 'Your only have 3 days to submit\n', 'the payment.After that the\n', 'price will be doubled.\n', "Also,if you don't be able to\n", 'recover your file forever.\n', 'We will have free events for user who \n', "are so poor that they couldn't pay in 6 months.\n"]
>>> liter = iter(trlines)
>>> liter.next()
'What Happened to My Computer?\n'
>>> liter.next()
'Your important files are encrypted.\n'
>>> liter.next()
'Many of your documents,photos,videos,\n'
>>> liter.next()
'databases and others are no longer \n'
>>> liter.next()
'accesssible because they have been encrypted.\n'
>>> liter.next()
'Maybe you are busy looking for\n'
>>> liter.next()
'a wayto recover your files,\n'
>>> liter.next()
'but do not waste your time.\n'
>>> liter.next()
'Nobody can recover your files without our\n'
>>> liter.next()
'decryption service.Can I Recover My files?\n'
>>> liter.next()
'Sure,We guarantee that you can recover all\n'
>>> liter.next()
'your files safely and easily,\n'
>>> liter.next()
'But you have not so enouth time.\n'
>>> liter.next()
'you can decrypt some of your files and free.\n'
>>> liter.next()
'Try now by clicking <Decryt>.\n'
>>> liter.next()
'But if you want to decrypt all\n'
>>> liter.next()
'your files,you need to pay.\n'
>>> liter.next()
'Your only have 3 days to submit\n'
>>> liter.next()
'the payment.After that the\n'
>>> liter.next()
'price will be doubled.\n'
>>> liter.next()
"Also,if you don't be able to\n"
>>> liter.next()
'recover your file forever.\n'
>>> liter.next()
'We will have free events for user who \n'
>>> liter.next()
"are so poor that they couldn't pay in 6 months.\n"
>>> liter.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>>treadlines.close()

一直將文本文件中的內容迭代完畢之後,出現了一個停止迭代的路徑追蹤。
當然,這個文本還不算很長,我是用手動的方式將文本中的內容輸出完畢。但是加入文本很長,怎麼做,沒錯,是使用循環while

>>> treadlines = open("/pythonFile/file/rtext.txt")
>>> trlines = treadlines.readlines()
>>> liter = iter(trlines)
>>> while True:
...     print liter.next(),
... 
What Happened to My Computer?
Your important files are encrypted.
Many of your documents,photos,videos,
databases and others are no longer 
accesssible because they have been encrypted.
Maybe you are busy looking for
a wayto recover your files,
but do not waste your time.
Nobody can recover your files without our
decryption service.Can I Recover My files?
Sure,We guarantee that you can recover all
your files safely and easily,
But you have not so enouth time.
you can decrypt some of your files and free.
Try now by clicking <Decryt>.
But if you want to decrypt all
your files,you need to pay.
Your only have 3 days to submit
the payment.After that the
price will be doubled.
Also,if you don't be able to
recover your file forever.
We will have free events for user who 
are so poor that they couldn't pay in 6 months.
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
StopIteration
>>> treadlines.close()

當然迭代器並非像上面的示例一樣簡單,這裏只是簡單的舉一些示例。在Python中,還可以將文本中的迭代和列表、元組互用。

>>> treadlines = open("/pythonFile/file/rtext.txt")
>>> list(treadlines)
['What Happened to My Computer?\n', 'Your important files are encrypted.\n', 'Many of your documents,photos,videos,\n', 'databases and others are no longer \n', 'accesssible because they have been encrypted.\n', 'Maybe you are busy looking for\n', 'a wayto recover your files,\n', 'but do not waste your time.\n', 'Nobody can recover your files without our\n', 'decryption service.Can I Recover My files?\n', 'Sure,We guarantee that you can recover all\n', 'your files safely and easily,\n', 'But you have not so enouth time.\n', 'you can decrypt some of your files and free.\n', 'Try now by clicking <Decryt>.\n', 'But if you want to decrypt all\n', 'your files,you need to pay.\n', 'Your only have 3 days to submit\n', 'the payment.After that the\n', 'price will be doubled.\n', "Also,if you don't be able to\n", 'recover your file forever.\n', 'We will have free events for user who \n', "are so poor that they couldn't pay in 6 months.\n"]
>>> tuple(treadlines)
()                            #爲什麼什麼都沒有返回???
>>> treadlines.close()

上述示例中tuple並沒有返回任何值,是因爲先進行了List操作,文件中的指針已經指向文件的末尾了,所以tuple沒有輸出任何內容。如果重新讓其輸出,則需要重新給對象賦值。
如下:

>>> treadlines = open("/pythonFile/file/rtext.txt")
>>> tuple(treadlines)
('What Happened to My Computer?\n', 'Your important files are encrypted.\n', 'Many of your documents,photos,videos,\n', 'databases and others are no longer \n', 'accesssible because they have been encrypted.\n', 'Maybe you are busy looking for\n', 'a wayto recover your files,\n', 'but do not waste your time.\n', 'Nobody can recover your files without our\n', 'decryption service.Can I Recover My files?\n', 'Sure,We guarantee that you can recover all\n', 'your files safely and easily,\n', 'But you have not so enouth time.\n', 'you can decrypt some of your files and free.\n', 'Try now by clicking <Decryt>.\n', 'But if you want to decrypt all\n', 'your files,you need to pay.\n', 'Your only have 3 days to submit\n', 'the payment.After that the\n', 'price will be doubled.\n', "Also,if you don't be able to\n", 'recover your file forever.\n', 'We will have free events for user who \n', "are so poor that they couldn't pay in 6 months.\n")
>>> treadlines.close()

關於文件操作的內容,暫時學習這麼多,從明天開始學習函數。

函數的內容多也很重要,好好學習。加油。

2017年05月14日
下午15:19

發佈了47 篇原創文章 · 獲贊 16 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章