Python學習之文件(一)

我們知道,無論是在存儲介質上,還是在雲端,我們所有的對象的都是以文件的形式存在的,尤其是在Linux系統中,一切都文件。
所以,文件在Python中也是一種數據類型。
那麼,文件除了文本文件以外,還有視頻,音頻,圖片,以及很多我們沒有見過擴展名的文件。
那麼在Python中,文件依然是一個很重要的知識,因爲我們在寫程序的時候,或者使用程序調用文件的時候,不可能在命令行直接輸入那麼多代碼,最終是要將寫好的代碼進行調試並進行封裝。那麼,封裝起來的這些代碼還有代碼中要調用的文本、圖片、視頻、音頻都是文件。
從這一個節開始時,學習文件。

文件

那麼在Python中,文件都有那些屬性和函數呢?

>>> 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']
>>> 

從以上列出的可以看出,文件有很多屬性和函數,但是我們不可能在這裏闡述所有文件的屬性和函數,我們只是針對文件的部分和函數進行學習。

打開文件

help(open)
Help on built-in function open in module builtin:

open(…)
open(name[, mode[, buffering]]) -> file object

Open a file using the file() type, returns a file object.  This is the
preferred way to open a file.

(END)
解釋:首選打開文件的方式,使用file( )類型打開一個文件,返回一個文件對象。
open(name)打開一個名爲name的文件。
open(name[,mode])以何種模式打開名爲name的文件。
筆者使用的系統是Linux系統,所以我所有的文件創建都是在Linux中進行的。 使用的是VI編輯器。

需求,在/pythonFile/file路徑下創建一個名爲test.txt的文件,並打開讀取出文件中的內容。
這個文件中的內容爲如下:

[root@python file]# cat test.txt 
#!/usr/bin/env python

#coding:utf-8

this is first file;
this is test file;
the file will be opend;
Please close file if you opened file when you don't used it;
[root@python file]# 

使用Pyothon中的open命令打開文件。

>>> tfile = open("/pythonFile/file/test.txt")
>>> for text in tfile:
...     print text
... 
#!/usr/bin/env python



#coding:utf-8



this is first file;

this is test file;

the file will be opend;

Please close file if you opened file when you don't used it;

>>> 

說明:上述代碼中首先定義了一個使用open打開文件的對象,然後通過for循環輸出文件中的內容
注意我們發現在上面執行後輸出的文本中和原先的文本的差別有點大
原因是:原文本中每一行的後面都有一個換行符“”\n“”當我們把打開文件的對象通過for循環輸出時,print 後面也是默認有一個換行符“”\n“”,所以,相當於有兩個“”\n\n“”,所以,輸出的打開文件的對象時,再次輸出一個換行符
那麼再print後面在加一個逗號再來進行以下比較。

>>> tfile = open("/pythonFile/file/test.txt")
>>> for text in tfile:
...     print text,
... 
#!/usr/bin/env python

#coding:utf-8

this is first file;
this is test file;
the file will be opend;
Please close file if you opened file when you don't used it;
>>> 

注意,我們可能會遇到在這樣一種狀況,那就是,當我們再次輸出這個文件中的內容時,會什麼也得不到。
來試一下。

>>> tfile = open("/pythonFile/file/test.txt")
>>> for text in tfile:
...     print text,
... 
#!/usr/bin/env python

#coding:utf-8

this is first file;
this is test file;
the file will be opend;
Please close file if you opened file when you don't used it;
>>> for line in tfile:
...     print line,
... 
>>> 
>>> for text in tfile:
...      print text,
... 
>>> 

發現,並不是由於變量的原因而導致文本內容無法輸出。
其實這個在python中,Python它並不認爲這個有錯,因爲它是爲你考慮,它擔心你在文件的後面追加了內容,所以,當你 再次輸出文件的時候,指針停留在上一次打開文件的末尾。假如在text中再加入這麼一行

[root@python file]# cat test.txt 
#!/usr/bin/env python

#coding:utf-8

this is first file;
this is test file;
the file will be opend;
Please close file if you opened file when you don't used it;
這是追加的一行內容,用來做測試。
[root@python file]# 

上述代碼中的中文爲新寫入的內容

for text in tfile:
… print text,

但是在執行for循環後依然沒有得到文件中的內容,這是爲什麼呢??
接下來找原因

創建文件

上面我們學習瞭如何打開已經存在的文件,但是我們要怎麼創建一個文件呢?這個做法在我們寫網絡爬蟲的時候特別需要,比如我們要爬蟲一個網頁的所有內容,並把它保存在存儲當中。
可以這樣做:

>>> touch = open("/pythonFile/file/touch.txt","w")
>>> touch.write("這是一個測試創建的文本文件")
>>> 

查看文件:
[root@python file]# cat touch.txt
這是一個測試創建的文本文件[root@python file]#

但是這裏注意,自己創建並寫入的測試語句會在文本文件中看不到,那麼怎麼做才能讓自己看到呢?
這樣做,重新打開文件,
我個人理解,由於Python中,當執行write的時候,Python會認爲你正在文件中寫東西,當你寫東西的時候它不會將內容輸入到文件中,只有當你執行打開操作或者讀操作的時候,Python纔會認爲你已經完成文件內容的寫操作,所以,只有當再次執行open或者read的時候,write的內容纔會被寫進文件中。

>>> touch = open("/pythonFile/file/touch.txt","w")
>>> touch.write("這是一個測試創建的文本文件")
>>> touch.write("I finished this time")
>>> touch = open("/pythonFile/file/touch.txt")

文件查看:
[root@python file]# ls
test.txt touch.txt
[root@python file]# cat touch.txt
This is test read operate[root@python file]#

發現,我們再創建文件的時候,依然是使用的open模式,但是和之前的open不同的是,我們使用了open中的模式,多加了一個w,這是在告訴Python,我們在以什麼樣的模式打開文件。

附:open中的打開模式

r 以讀取方式打開文件,可以讀取文件中的信息
w 以寫入的方式打開問阿金,可以向文件中寫入信息。如果文件存在,則清空文件,在寫入新的內容。
a 以追加的模式打開文件(打開文件,文件指針自動移動到文件末尾),如果文件不存在,則創建
r+ 以讀寫的方式打開文件,可以對文件進行讀取和寫入操作。
w+ 消除文件內容,然後以讀寫方式打開文件
a+ 以讀寫方式打開文件,並且將文件中的指針移動到末尾
b binary,即以二進制模式打開文件,而不是以文本的模式。該模式只對windows和DOS有效,類unix的文件是用二進制模式進行操作的。

那麼上面我們使用open默認的模式是以什麼樣的方式打開的文件呢。我們可以通過直接輸入文件的對象名可以查看,如下:

>>> file = open("/pythonFile/file/test.txt")
>>> file
<open file '/pythonFile/file/test.txt', mode 'r' at 0x1f430c0>
>>> 

發現,使用默認的open文件是以r即只讀的方式打開的。
既然這樣,那就練習一下幾種文件的追加模式吧!
前提:我用的是Linux系統,我在/pythonFile/file/路徑下創建了一個文件touch.txt裏面的內容如下:
[root@python file]# cat touch.This is test text
這是一個測試文本文件
以下的所有模式的操作都以這個文件爲測試文件。
[root@python file]#
以下測試只有代碼,不進行解釋,如果文件內容有改變,都是依次按照順序進行測試的。

1、以*w方式打開文件。*

w 以寫入的方式打開問阿金,可以向文件中寫入信息。如果文件存在,則清空文件,在寫入新的內容。

touch = open(“/pythonFile/file/touch.txt”)
for text in touch:
… print text,

This is test text
這是一個測試文本文件
以下的所有模式的操作都以這個文件爲測試文件。
touch = open(“/pythonFile/file/touch.txt”,”w”) #文件存在,則清空文件內容,重新寫入
touch.write(“This is writing operate”)
touch = open(“/pythonFile/file/touch.txt”)
for text in touch:
… print text,

This is writing operate #驗證結果一樣

2、以*a模式打開文件*
a 以追加的模式打開文件(打開文件,文件指針自動移動到文件末尾),如果文件不存在,則創建
1)當文件存在

>>> touch = open("/pythonFile/file/touch.txt","a")
>>> touch.write("這個文件是存在的,這一條是追加進去的 !") 
>>> touch = open("/pythonFile/file/touch.txt")
>>> for text in touch:
...     print text,
... 
This is writing operate這個文件是存在的,這一條是追加進去的 !
>>> 

2)當文件不存在

>>> appendtest = open("/pythonFile/file/app.txt","a")
>>> appendtest.write("this file is not exsits in system,\nthis is new created file")
>>> appendtest = open("/pythonFile/file/app.txt")
>>> for text in appendtest:
...     print text,
... 
this file is not exsits in system,
this is new created file
>>> 

3、以讀寫的方式打開文件

>>> rtest = open("/pythonFile/file/touch.txt","r+")
>>> rtest.write("This is writing in operate")
>>> rtest = open("/pythonFile/file/touch.txt")
>>> rtest.read()
'This is writing in operate\xe4\xb8\xaa\xe6\x96\x87\xe4\xbb\xb6\xe6\x98\xaf\xe5\xad\x98\xe5\x9c\xa8\xe7\x9a\x84\xef\xbc\x8c\xe8\xbf\x99\xe4\xb8\x80\xe6\x9d\xa1\xe6\x98\xaf\xe8\xbf\xbd\xe5\x8a\xa0\xe8\xbf\x9b\xe5\x8e\xbb\xe7\x9a\x84 \xef\xbc\x81'

在這裏我用了read函數,但是一下子後面奔出了這麼多16進制的符號,讓我蒙圈了,所以我就看了一下幫助文檔。文檔是這麼寫的:

help(file.read)
Help on built-in function read:

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)
解釋:如果size參數爲負數或省略,則直到達到EOF爲止。
請注意,在非阻塞模式下,數據少於請求的數據
即使沒有給出大小參數,也可以返回

反正就是這樣,自己看看。後面接觸到再進行詳細學習。
其餘的模式再不一一做詳細介紹了

使用with

既然,文件能夠被打開,那麼,文件當然也可以關閉,在Python中,如果使用了打開文件的模式,一定要在最後記得關閉文件,而關閉文件使用的則是close( )函數

>>> file = open("/pythonFile/file/app.txt")
>>> file.close()
>>> for text in file:
...     print text,
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file
>>> 

發現,已經讀不出文件了,提示說明:已關閉文件的上的I/O操作。
那麼來看一下close( )函數

help(file.close)
Help on built-in function close:

close(…)
close() -> None or (perhaps) an integer. Close the file.

Sets data attribute .closed to True.  A closed file cannot be used for
further I/O operations.  close() may be called more than once without
error.  Some kinds of file objects (for example, opened by popen())
may return an exit status upon closing.

:

 解釋:將數據屬性.closed設置爲True。 關閉文件不能用於
 進一步的I / O操作。 close()可能被調用不止一次
 錯誤。 某些類型的文件對象(例如,由popen()打開)
 關閉時可能返回退出狀態。

但是,我們有時候對文件進行了打開操作後,很容易忘記對文件進行關閉,怎麼辦,使用with

>>> wfile = open("/pythonFile/file/app.txt")
>>> for text in wfile:
...     print text,
... 
this file is not exsits in system,
this is new created file
>>> wfile.close()
>>> with open("/pythonFile/file/app.txt","a") as wf:
...     wf.write("\n這是一個測試with功能的語句\n")
... 
>>> with open("/pythonFile/file/app.txt","r") as rf:
...     print rf.read()
... 
this file is not exsits in system,
this is new created file

這是一個測試with功能的語句

文件的狀態

無論什麼對象,都會有一個狀態,生物的狀態就是活着還是死了,自然的狀態是有生機還是死氣沉沉,太陽的狀態是爆發還是穩定等等,文件也是對象,那麼文件也會有創建日期,修改日期,訪問日期以及大小等,這些都是文件的狀態。在Python中,給我們提供了一種查看文件的 狀態的方法。那就是OS模塊
毫無疑問,我們再使用這個模塊之前,要做的首先是導入這個模塊。

>>> import os
>>> fstate = os.stat("/pythonFile/file/app.txt")
>>> print fstate
posix.stat_result(st_mode=33188, st_ino=261266, st_dev=2051L, st_nlink=1, st_uid=0, st_gid=0, st_size=99, st_atime=1494415804, st_mtime=1494415773, st_ctime=1494415773)
>>> 

其中st_mode爲文件的權限模式,st_ino爲文件的編inode編號,st_dev爲是否是塊設備文件,st_link爲是否是鏈接文件。st_uid爲所屬用戶的id,st_gid爲所屬用戶組的id,st_size爲文件的大小,st_atime爲文件最近訪問的時間,st_mtime爲文件最近修改的時間,st_ctime爲文件的創建時間。
st_atime=1494415804, st_mtime=1494415773, st_ctime=1494415773)這TMD到底是什麼時間,我第一次看到的時候也醉了,不知道是什麼時間,但是,幸好我可以藉助Python中提供的time模塊進行時間的查看,對比一下,一目瞭然。
文件的創建時間

>>>import time
>>> time.localtime(fstate.st_ctime)
time.struct_time(tm_year=2017, tm_mon=5, tm_mday=10, tm_hour=19, tm_min=29, tm_sec=33, tm_wday=2, tm_yday=130, tm_isdst=0)
>>> 

好了今天關於文件的學習到這裏就結束了,不過這只是文件的一部分,Python中,還有一些文件的知識,明天繼續。
最近一天計劃,先學習完文件,從下一週開始,學習函數。

完成於2017年05月14日
凌晨00:12

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