| 導航:起始頁 > Dive Into Python > 異常和文件處理 > 與目錄共事 | << >> | ||||
深入 Python :Dive Into Python 中文版Python 從新手到專家 [Dip_5.4b_CPyUG_Release] |
|||||
os.path 模塊有幾個操作文件和目錄的函數。這里,我們看看如何操作路徑名和列出一個目錄的內容。
>>> import os >>> os.path.join("c:\\music\\ap\\", "mahadeva.mp3")![]()
'c:\\music\\ap\\mahadeva.mp3' >>> os.path.join("c:\\music\\ap", "mahadeva.mp3")
'c:\\music\\ap\\mahadeva.mp3' >>> os.path.expanduser("~")
'c:\\Documents and Settings\\mpilgrim\\My Documents' >>> os.path.join(os.path.expanduser("~"), "Python")
'c:\\Documents and Settings\\mpilgrim\\My Documents\\Python'
| os.path 是一個模塊的引用;使用哪一個模塊要看你正運行在哪種平臺上。就像 getpass 通過將 getpass 設置為一個與平臺相關的函數從而封裝了平臺之間的不同。os 通過設置 path 封裝不同的相關平臺模塊。 | |
| os.path 的 join 函數把一個或多個部分路徑名連接成一個路徑名。在這個簡單的例子中,它只是將字符串進行連接。(請注意在 Windows 下處理路徑名是一個麻煩的事,因為反斜線字符必須被轉義。) | |
| 在這個幾乎沒有價值的例子中,在將路徑名加到文件名上之前,join 將在路徑名后添加額外的反斜線。當發現這一點時我高興極了,因為當用一種新的語言創建我自已的工具包時,addSlashIfNecessary 總是我必須要寫的那些愚蠢的小函數之一。在 Python 中不要 寫這樣的愚蠢的小函數,聰明的人已經為你考慮到了。 | |
| expanduser 將對使用 ~ 來表示當前用戶根目錄的路徑名進行擴展。在任何平臺上,只要用戶擁有一個根目錄,它就會有效,像 Windows、UNIX 和 Mac OS X,但在 Mac OS 上無效。 | |
| 將這些技術組合在一起,你可以容易地為在用戶根目錄下的目錄和文件構造出路徑名。 |
>>> os.path.split("c:\\music\\ap\\mahadeva.mp3")('c:\\music\\ap', 'mahadeva.mp3') >>> (filepath, filename) = os.path.split("c:\\music\\ap\\mahadeva.mp3")
>>> filepath
'c:\\music\\ap' >>> filename
'mahadeva.mp3' >>> (shortname, extension) = os.path.splitext(filename)
>>> shortname 'mahadeva' >>> extension '.mp3'
| split 函數對一個全路徑名進行分割,返回一個包含路徑和文件名的 tuple。還記得我說過你可以使用多變量賦值從一個函數返回多個值嗎?對,split 就是這樣一個函數。 | |
| 我們將 split 函數的返回值賦值給一個兩個變量的 tuple。每個變量接收到返回 tuple 相對應的元素值。 | |
| 第一個變量,filepath,接收到從 split 返回 tuple 的第一個元素的值,文件路徑。 | |
| 第二個變量,filename,接收到從 split 返回 tuple 的第二個元素的值,文件名。 | |
| os.path 也包含了一個 splitext 函數,可以用來對文件名進行分割,并且返回一個包含了文件名和文件擴展名的 tuple。我們使用相同的技術來將它們賦值給獨立的變量。 |
>>> os.listdir("c:\\music\\_singles\\")['a_time_long_forgotten_con.mp3', 'hellraiser.mp3', 'kairo.mp3', 'long_way_home1.mp3', 'sidewinder.mp3', 'spinning.mp3'] >>> dirname = "c:\\" >>> os.listdir(dirname)
['AUTOEXEC.BAT', 'boot.ini', 'CONFIG.SYS', 'cygwin', 'docbook', 'Documents and Settings', 'Incoming', 'Inetpub', 'IO.SYS', 'MSDOS.SYS', 'Music', 'NTDETECT.COM', 'ntldr', 'pagefile.sys', 'Program Files', 'Python20', 'RECYCLER', 'System Volume Information', 'TEMP', 'WINNT'] >>> [f for f in os.listdir(dirname) ... if os.path.isfile(os.path.join(dirname, f))]
['AUTOEXEC.BAT', 'boot.ini', 'CONFIG.SYS', 'IO.SYS', 'MSDOS.SYS', 'NTDETECT.COM', 'ntldr', 'pagefile.sys'] >>> [f for f in os.listdir(dirname) ... if os.path.isdir(os.path.join(dirname, f))]
['cygwin', 'docbook', 'Documents and Settings', 'Incoming', 'Inetpub', 'Music', 'Program Files', 'Python20', 'RECYCLER', 'System Volume Information', 'TEMP', 'WINNT']
| listdir 函數接收一個路徑名,并返回那個目錄的內容的 list。 | |
| listdir 同時返回文件和文件夾,并不指出哪個是文件,哪個是文件夾。 | |
| 你可以使用過濾列表和 os.path 模塊的 isfile 函數,從文件夾中將文件分離出來。isfile 接收一個路徑名,如果路徑表示一個文件,則返回 1,否則為 0。在這里,我們使用 os.path.join 來確保得到一個全路徑名,但 isfile 對部分路徑 (相對于當前目錄) 也是有效的。你可以使用 os.getcwd() 來得到當前目錄。 | |
| os.path 還有一個 isdir 函數,當路徑表示一個目錄,則返回 1,否則為 0。你可以使用它來得到一個目錄下的子目錄列表。 |
def listDirectory(directory, fileExtList): "get list of file info objects for files of particular extensions" fileList = [os.path.normcase(f) for f in os.listdir(directory)]![]()
fileList = [os.path.join(directory, f) for f in fileList if os.path.splitext(f)[1] in fileExtList]
![]()
![]()
| 只要有可能,你就應該使用在 os 和 os.path 中的函數進行文件、目錄和路徑的操作。這些模塊是對平臺相關模塊的封裝模塊,所以像 os.path.split 這樣的函數可以工作在 UNIX、Windows、Mac OS 和 Python 所支持的任一種平臺上。 | |
還有一種獲得目錄內容的方法。它非常強大,并使用了一些你在命令行上工作時可能已經熟悉的通配符。
>>> os.listdir("c:\\music\\_singles\\")['a_time_long_forgotten_con.mp3', 'hellraiser.mp3', 'kairo.mp3', 'long_way_home1.mp3', 'sidewinder.mp3', 'spinning.mp3'] >>> import glob >>> glob.glob('c:\\music\\_singles\\*.mp3')
['c:\\music\\_singles\\a_time_long_forgotten_con.mp3', 'c:\\music\\_singles\\hellraiser.mp3', 'c:\\music\\_singles\\kairo.mp3', 'c:\\music\\_singles\\long_way_home1.mp3', 'c:\\music\\_singles\\sidewinder.mp3', 'c:\\music\\_singles\\spinning.mp3'] >>> glob.glob('c:\\music\\_singles\\s*.mp3')
['c:\\music\\_singles\\sidewinder.mp3', 'c:\\music\\_singles\\spinning.mp3'] >>> glob.glob('c:\\music\\*\\*.mp3')
![]()
<< 使用 sys.modules |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
全部放在一起 >> |