python 获取文件列表_Python3 - 获取文件夹中的文件列表
問題
獲取文件系統中某個目錄下的所有文件列表。
解決方案
使用 os.listdir() 函數來獲取某個目錄中的文件列表,比如:
import os
file_name = os.listdir('/Users/xz/test')
print(file_name)
['Bath.txt', 'test.py', '2.txt', '1.txt', 'cook.txt']
結果會返回目錄中所有文件列表,包括所有文件,子目錄,符號鏈接等等。 如果需要通過某種方式過濾數據,可以考慮結合 os.path 庫中的一些函數來使用列表推導。比如:
import os.path
names = [name for name in os.listdir('/Users/xz/test')
if os.path.isfile(os.path.join('/Users/xz/test', name))]
print(names)
['Bath.txt', 'test.py', '2.txt', '1.txt', 'cook.txt']
字符串的 startswith() 和 endswith() 方法對于過濾一個目錄的內容也是很有用的。比如:
pyname = [name for name in os.listdir('/Users/xz/test') if name.endswith('.py')]
print(pyname)
['test.py']
對于文件名的匹配,你可能會考慮使用 glob 或 fnmatch 模塊。比如:
import glob
pyname = glob.glob('/Users/xz/test/*.py')
print(pyname)
['/Users/xz/test/test.py']
from fnmatch import fnmatch
pyname = [name for name in os.listdir('/Users/xz/test') if fnmatch(name, '*.py')]
print(pyname)
['test.py']
討論
通過上述的幾種方法,均可以獲取目錄中的文件列表,但是其返回結果只是目錄中實體名列表而已。
如果想獲取文件的其他元數據,比如文件大小,修改時間等等,需要使用到 os.path 模塊中的函數,或os.stat() 函數來收集數據。比如:
# Get file sizes and modification dates
name_sz_dt = [(name, os.path.getsize(name), ar.get(os.path.getmtime(name)).format("YYYY-MM-DD HH:mm:ss"))
for name in pyfile]
for name, sizes, date in name_sz_dt:
print(name, sizes, date)
/Users/xz/test/test.py 214 2018-11-29 14:03:02
# Alternative: Get file metadata
file_metadata = [(name, os.stat(name)) for name in pyfile]
for name, meta in file_metadata:
print(name, meta.st_size, ar.get(meta.st_mtime).format("YYYY-MM-DD HH:mm:ss"))
/Users/xz/test/test.py 214 2018-11-29 14:03:02
需要注意的是,有時候在處理文件名編碼問題時,可能會出現一些問題。 通常,函數 os.listdir()返回的實體列表是根據系統默認的文件名編碼進行解碼。 但有時候也會遇到一些不能正常解碼的文件名。
總結
以上是生活随笔為你收集整理的python 获取文件列表_Python3 - 获取文件夹中的文件列表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vba php,VBA
- 下一篇: python 数字类型和字符串类型的相互