[英]How do I list all files of a directory?
如何列出 Python 中目錄的所有文件並將它們添加到list
?
os.listdir()
將為您提供目錄中的所有內容 -文件和目錄。
如果你只想要文件,你可以使用os.path
過濾掉它:
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
或者您可以使用os.walk()
它將為它訪問的每個目錄生成兩個列表- 為您拆分為文件和目錄。 如果你只想要頂級目錄,你可以在它第一次產生時打破
from os import walk
f = []
for (dirpath, dirnames, filenames) in walk(mypath):
f.extend(filenames)
break
或者,更短:
from os import walk
filenames = next(walk(mypath), (None, None, []))[2] # [] if no file
我更喜歡使用glob
模塊,因為它進行模式匹配和擴展。
import glob
print(glob.glob("/home/adam/*"))
它直觀地進行模式匹配
import glob
# All files ending with .txt
print(glob.glob("/home/adam/*.txt"))
# All files ending with .txt with depth of 2 folder
print(glob.glob("/home/adam/*/*.txt"))
它將返回一個包含查詢文件的列表:
['/home/adam/file1.txt', '/home/adam/file2.txt', .... ]
os.listdir()
- 當前目錄中的列表
使用 os 模塊中的 listdir,您可以獲得當前目錄中的文件和文件夾
import os
arr = os.listdir()
print(arr)
>>> ['$RECYCLE.BIN', 'work.txt', '3ebooks.txt', 'documents']
arr = os.listdir('c:\\files')
glob
從水珠
使用 glob,您可以指定要列出的文件類型,如下所示
import glob
txtfiles = []
for file in glob.glob("*.txt"):
txtfiles.append(file)
glob
mylist = [f for f in glob.glob("*.txt")]
import os
from os import listdir
from os.path import isfile, join
cwd = os.getcwd()
onlyfiles = [os.path.join(cwd, f) for f in os.listdir(cwd) if
os.path.isfile(os.path.join(cwd, f))]
print(onlyfiles)
['G:\\getfilesname\\getfilesname.py', 'G:\\getfilesname\\example.txt']
使用
os.path.abspath
獲取完整路徑名
你得到完整的路徑作為回報
import os
files_path = [os.path.abspath(x) for x in os.listdir()]
print(files_path)
['F:\\documenti\applications.txt', 'F:\\documenti\collections.txt']
Walk:遍歷子目錄
os.walk 返回根目錄、目錄列表和文件列表,這就是為什么我在 for 循環中將它們解壓到 r、d、f 中的原因; 然后,它會在根目錄的子文件夾中查找其他文件和目錄,依此類推,直到沒有子文件夾為止。
import os
# Getting the current work directory (cwd)
thisdir = os.getcwd()
# r=root, d=directories, f = files
for r, d, f in os.walk(thisdir):
for file in f:
if file.endswith(".docx"):
print(os.path.join(r, file))
os.listdir()
:獲取當前目錄中的文件(Python 2)
在 Python 2 中,如果您想要當前目錄中的文件列表,您必須將參數指定為 '.' 或 os.listdir 方法中的 os.getcwd()。
import os
arr = os.listdir('.')
print(arr)
>>> ['$RECYCLE.BIN', 'work.txt', '3ebooks.txt', 'documents']
進入目錄樹
# Method 1
x = os.listdir('..')
# Method 2
x= os.listdir('/')
獲取文件:特定目錄中的
os.listdir()
(Python 2 和 3)
import os
arr = os.listdir('F:\\python')
print(arr)
>>> ['$RECYCLE.BIN', 'work.txt', '3ebooks.txt', 'documents']
使用
os.listdir()
獲取特定子目錄的文件
import os
x = os.listdir("./content")
os.walk('.')
- 當前目錄
import os
arr = next(os.walk('.'))[2]
print(arr)
>>> ['5bs_Turismo1.pdf', '5bs_Turismo1.pptx', 'esperienza.txt']
next(os.walk('.'))
和os.path.join('dir', 'file')
import os
arr = []
for d,r,f in next(os.walk("F:\\_python")):
for file in f:
arr.append(os.path.join(r,file))
for f in arr:
print(files)
>>> F:\\_python\\dict_class.py
>>> F:\\_python\\programmi.txt
next(os.walk('F:\\\\')
- 獲取完整路徑 - 列表理解
[os.path.join(r,file) for r,d,f in next(os.walk("F:\\_python")) for file in f]
>>> ['F:\\_python\\dict_class.py', 'F:\\_python\\programmi.txt']
os.walk
- 獲取完整路徑 - 子目錄中的所有文件**
x = [os.path.join(r,file) for r,d,f in os.walk("F:\\_python") for file in f]
print(x)
>>> ['F:\\_python\\dict.py', 'F:\\_python\\progr.txt', 'F:\\_python\\readl.py']
os.listdir()
- 只獲取 txt 文件
arr_txt = [x for x in os.listdir() if x.endswith(".txt")]
print(arr_txt)
>>> ['work.txt', '3ebooks.txt']
使用
glob
獲取文件的完整路徑
如果我需要文件的絕對路徑:
from path import path
from glob import glob
x = [path(f).abspath() for f in glob("F:\\*.txt")]
for f in x:
print(f)
>>> F:\acquistionline.txt
>>> F:\acquisti_2018.txt
>>> F:\bootstrap_jquery_ecc.txt
使用
os.path.isfile
避免列表中的目錄
import os.path
listOfFiles = [f for f in os.listdir() if os.path.isfile(f)]
print(listOfFiles)
>>> ['a simple game.py', 'data.txt', 'decorator.py']
使用 Python 3.4 中的
pathlib
import pathlib
flist = []
for p in pathlib.Path('.').iterdir():
if p.is_file():
print(p)
flist.append(p)
>>> error.PNG
>>> exemaker.bat
>>> guiprova.mp3
>>> setup.py
>>> speak_gui2.py
>>> thumb.PNG
使用list comprehension
:
flist = [p for p in pathlib.Path('.').iterdir() if p.is_file()]
或者,使用pathlib.Path()
而不是pathlib.Path(".")
在 pathlib.Path() 中使用 glob 方法
import pathlib
py = pathlib.Path().glob("*.py")
for file in py:
print(file)
>>> stack_overflow_list.py
>>> stack_overflow_list_tkinter.py
使用 os.walk 獲取所有且唯一的文件
import os
x = [i[2] for i in os.walk('.')]
y=[]
for t in x:
for f in t:
y.append(f)
print(y)
>>> ['append_to_list.py', 'data.txt', 'data1.txt', 'data2.txt', 'data_180617', 'os_walk.py', 'READ2.py', 'read_data.py', 'somma_defaltdic.py', 'substitute_words.py', 'sum_data.py', 'data.txt', 'data1.txt', 'data_180617']
僅使用 next 獲取文件並進入目錄
import os
x = next(os.walk('F://python'))[2]
print(x)
>>> ['calculator.bat','calculator.py']
僅使用 next 獲取目錄並進入目錄
import os
next(os.walk('F://python'))[1] # for the current dir use ('.')
>>> ['python3','others']
使用
walk
獲取所有子目錄名稱
for r,d,f in os.walk("F:\\_python"):
for dirs in d:
print(dirs)
>>> .vscode
>>> pyexcel
>>> pyschool.py
>>> subtitles
>>> _metaprogramming
>>> .ipynb_checkpoints
os.scandir()
來自 Python 3.5 及更高版本
import os
x = [f.name for f in os.scandir() if f.is_file()]
print(x)
>>> ['calculator.bat','calculator.py']
# Another example with scandir (a little variation from docs.python.org)
# This one is more efficient than os.listdir.
# In this case, it shows the files only in the current directory
# where the script is executed.
import os
with os.scandir() as i:
for entry in i:
if entry.is_file():
print(entry.name)
>>> ebookmaker.py
>>> error.PNG
>>> exemaker.bat
>>> guiprova.mp3
>>> setup.py
>>> speakgui4.py
>>> speak_gui2.py
>>> speak_gui3.py
>>> thumb.PNG
例子:
前任。 1:子目錄下有多少個文件?
在此示例中,我們查找包含在所有目錄及其子目錄中的文件數。
import os
def count(dir, counter=0):
"returns number of files in dir and subdirs"
for pack in os.walk(dir):
for f in pack[2]:
counter += 1
return dir + " : " + str(counter) + "files"
print(count("F:\\python"))
>>> 'F:\\\python' : 12057 files'
例 2:如何將所有文件從一個目錄復制到另一個目錄?
一個腳本,用於在您的計算機中查找某種類型的所有文件(默認:pptx)並將它們復制到一個新文件夾中。
import os
import shutil
from path import path
destination = "F:\\file_copied"
# os.makedirs(destination)
def copyfile(dir, filetype='pptx', counter=0):
"Searches for pptx (or other - pptx is the default) files and copies them"
for pack in os.walk(dir):
for f in pack[2]:
if f.endswith(filetype):
fullpath = pack[0] + "\\" + f
print(fullpath)
shutil.copy(fullpath, destination)
counter += 1
if counter > 0:
print('-' * 30)
print("\t==> Found in: `" + dir + "` : " + str(counter) + " files\n")
for dir in os.listdir():
"searches for folders that starts with `_`"
if dir[0] == '_':
# copyfile(dir, filetype='pdf')
copyfile(dir, filetype='txt')
>>> _compiti18\Compito Contabilità 1\conti.txt
>>> _compiti18\Compito Contabilità 1\modula4.txt
>>> _compiti18\Compito Contabilità 1\moduloa4.txt
>>> ------------------------
>>> ==> Found in: `_compiti18` : 3 files
前任。 3:如何獲取一個txt文件中的所有文件
如果您想創建一個包含所有文件名的 txt 文件:
import os
mylist = ""
with open("filelist.txt", "w", encoding="utf-8") as file:
for eachfile in os.listdir():
mylist += eachfile + "\n"
file.write(mylist)
示例:包含硬盤驅動器所有文件的txt
"""
We are going to save a txt file with all the files in your directory.
We will use the function walk()
"""
import os
# see all the methods of os
# print(*dir(os), sep=", ")
listafile = []
percorso = []
with open("lista_file.txt", "w", encoding='utf-8') as testo:
for root, dirs, files in os.walk("D:\\"):
for file in files:
listafile.append(file)
percorso.append(root + "\\" + file)
testo.write(file + "\n")
listafile.sort()
print("N. of files", len(listafile))
with open("lista_file_ordinata.txt", "w", encoding="utf-8") as testo_ordinato:
for file in listafile:
testo_ordinato.write(file + "\n")
with open("percorso.txt", "w", encoding="utf-8") as file_percorso:
for file in percorso:
file_percorso.write(file + "\n")
os.system("lista_file.txt")
os.system("lista_file_ordinata.txt")
os.system("percorso.txt")
C:\\ 的所有文件在一個文本文件中
這是先前代碼的較短版本。 如果您需要從其他位置開始,請更改開始查找文件的文件夾。 此代碼在我的計算機上生成一個 50 mb 的文本文件,其中包含完整路徑的文件少於 500.000 行。
import os
with open("file.txt", "w", encoding="utf-8") as filewrite:
for r, d, f in os.walk("C:\\"):
for file in f:
filewrite.write(f"{r + file}\n")
如何在類型的文件夾中寫入包含所有路徑的文件
使用此功能,您可以創建一個 txt 文件,該文件將具有您要查找的文件類型的名稱(例如 pngfile.txt)以及該類型所有文件的所有完整路徑。 我想,它有時很有用。
import os
def searchfiles(extension='.ttf', folder='H:\\'):
"Create a txt file with all the file of a type"
with open(extension[1:] + "file.txt", "w", encoding="utf-8") as filewrite:
for r, d, f in os.walk(folder):
for file in f:
if file.endswith(extension):
filewrite.write(f"{r + file}\n")
# looking for png file (fonts) in the hard disk H:\
searchfiles('.png', 'H:\\')
>>> H:\4bs_18\Dolphins5.png
>>> H:\4bs_18\Dolphins6.png
>>> H:\4bs_18\Dolphins7.png
>>> H:\5_18\marketing html\assets\imageslogo2.png
>>> H:\7z001.png
>>> H:\7z002.png
(新)查找所有文件並使用 tkinter GUI 打開它們
我只是想在這個 2019 年添加一個小應用程序來搜索目錄中的所有文件,並能夠通過雙擊列表中的文件名來打開它們。
import tkinter as tk
import os
def searchfiles(extension='.txt', folder='H:\\'):
"insert all files in the listbox"
for r, d, f in os.walk(folder):
for file in f:
if file.endswith(extension):
lb.insert(0, r + "\\" + file)
def open_file():
os.startfile(lb.get(lb.curselection()[0]))
root = tk.Tk()
root.geometry("400x400")
bt = tk.Button(root, text="Search", command=lambda:searchfiles('.png', 'H:\\'))
bt.pack()
lb = tk.Listbox(root)
lb.pack(fill="both", expand=1)
lb.bind("<Double-Button>", lambda x: open_file())
root.mainloop()
import os
os.listdir("somedirectory")
將返回“somedirectory”中所有文件和目錄的列表。
僅獲取文件列表(無子目錄)的單行解決方案:
filenames = next(os.walk(path))[2]
或絕對路徑名:
paths = [os.path.join(path, fn) for fn in next(os.walk(path))[2]]
從目錄及其所有子目錄獲取完整文件路徑
import os
def get_filepaths(directory):
"""
This function will generate the file names in a directory
tree by walking the tree either top-down or bottom-up. For each
directory in the tree rooted at directory top (including top itself),
it yields a 3-tuple (dirpath, dirnames, filenames).
"""
file_paths = [] # List which will store all of the full filepaths.
# Walk the tree.
for root, directories, files in os.walk(directory):
for filename in files:
# Join the two strings in order to form the full filepath.
filepath = os.path.join(root, filename)
file_paths.append(filepath) # Add it to the list.
return file_paths # Self-explanatory.
# Run the above function and store its results in a variable.
full_file_paths = get_filepaths("/Users/johnny/Desktop/TEST")
print full_file_paths
將打印列表:
['/Users/johnny/Desktop/TEST/file1.txt', '/Users/johnny/Desktop/TEST/file2.txt', '/Users/johnny/Desktop/TEST/SUBFOLDER/file3.dat']
如果您願意,您可以打開並閱讀內容,或僅關注擴展名為“.dat”的文件,如下面的代碼所示:
for f in full_file_paths:
if f.endswith(".dat"):
print f
/Users/johnny/Desktop/TEST/SUBFOLDER/file3.dat
從 3.4 版開始,就有了比os.listdir()
更高效的內置迭代器:
pathlib
: 3.4 版中的新功能。
>>> import pathlib
>>> [p for p in pathlib.Path('.').iterdir() if p.is_file()]
根據PEP 428 , pathlib
庫的目的是提供一個簡單的類層次結構來處理文件系統路徑和用戶對它們進行的常見操作。
os.scandir()
: 3.5 版中的新功能。
>>> import os
>>> [entry for entry in os.scandir('.') if entry.is_file()]
請注意, os.walk()
使用os.scandir()
而不是 3.5 版中的os.listdir()
,根據PEP 471 ,其速度提高了 2-20 倍。
讓我也推薦閱讀下面的 ShadowRanger 評論。
當被問到這個問題時,我想Python 2是LTS版本,但是代碼示例將由Python 3 ( .5 ) 運行(我將盡可能使它們與Python 2兼容;此外,任何屬於我要發布的Python來自v3.5.4 - 除非另有說明)。 這會產生與問題中的另一個關鍵字相關的后果:“將它們添加到列表中”:
>>> import sys >>> sys.version '2.7.10 (default, Mar 8 2016, 15:02:46) [MSC v.1600 64 bit (AMD64)]' >>> m = map(lambda x: x, [1, 2, 3]) # Just a dummy lambda function >>> m, type(m) ([1, 2, 3], <type 'list'>) >>> len(m) 3
>>> import sys >>> sys.version '3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)]' >>> m = map(lambda x: x, [1, 2, 3]) >>> m, type(m) (<map object at 0x000001B4257342B0>, <class 'map'>) >>> len(m) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: object of type 'map' has no len() >>> lm0 = list(m) # Build a list from the generator >>> lm0, type(lm0) ([1, 2, 3], <class 'list'>) >>> >>> lm1 = list(m) # Build a list from the same generator >>> lm1, type(lm1) # Empty list now - generator already consumed ([], <class 'list'>)
這些示例將基於名為root_dir的目錄,具有以下結構(此示例適用於Win ,但我也在Lnx上使用相同的樹):
E:\\Work\\Dev\\StackOverflow\\q003207219>tree /f "root_dir" Folder PATH listing for volume Work Volume serial number is 00000029 3655:6FED E:\\WORK\\DEV\\STACKOVERFLOW\\Q003207219\\ROOT_DIR ¦ file0 ¦ file1 ¦ +---dir0 ¦ +---dir00 ¦ ¦ ¦ file000 ¦ ¦ ¦ ¦ ¦ +---dir000 ¦ ¦ file0000 ¦ ¦ ¦ +---dir01 ¦ ¦ file010 ¦ ¦ file011 ¦ ¦ ¦ +---dir02 ¦ +---dir020 ¦ +---dir0200 +---dir1 ¦ file10 ¦ file11 ¦ file12 ¦ +---dir2 ¦ ¦ file20 ¦ ¦ ¦ +---dir20 ¦ file200 ¦ +---dir3
[Python 3]:操作系統。listdir ( path='.' )
返回一個包含路徑給定目錄中條目名稱的列表。 該列表按任意順序排列,不包括特殊條目
'.'
和'..'
...
>>> import os >>> root_dir = "root_dir" # Path relative to current dir (os.getcwd()) >>> >>> os.listdir(root_dir) # List all the items in root_dir ['dir0', 'dir1', 'dir2', 'dir3', 'file0', 'file1'] >>> >>> [item for item in os.listdir(root_dir) if os.path.isfile(os.path.join(root_dir, item))] # Filter items and only keep files (strip out directories) ['file0', 'file1']
一個更詳細的例子( code_os_listdir.py ):
import os from pprint import pformat def _get_dir_content(path, include_folders, recursive): entries = os.listdir(path) for entry in entries: entry_with_path = os.path.join(path, entry) if os.path.isdir(entry_with_path): if include_folders: yield entry_with_path if recursive: for sub_entry in _get_dir_content(entry_with_path, include_folders, recursive): yield sub_entry else: yield entry_with_path def get_dir_content(path, include_folders=True, recursive=True, prepend_folder_name=True): path_len = len(path) + len(os.path.sep) for item in _get_dir_content(path, include_folders, recursive): yield item if prepend_folder_name else item[path_len:] def _get_dir_content_old(path, include_folders, recursive): entries = os.listdir(path) ret = list() for entry in entries: entry_with_path = os.path.join(path, entry) if os.path.isdir(entry_with_path): if include_folders: ret.append(entry_with_path) if recursive: ret.extend(_get_dir_content_old(entry_with_path, include_folders, recursive)) else: ret.append(entry_with_path) return ret def get_dir_content_old(path, include_folders=True, recursive=True, prepend_folder_name=True): path_len = len(path) + len(os.path.sep) return [item if prepend_folder_name else item[path_len:] for item in _get_dir_content_old(path, include_folders, recursive)] def main(): root_dir = "root_dir" ret0 = get_dir_content(root_dir, include_folders=True, recursive=True, prepend_folder_name=True) lret0 = list(ret0) print(ret0, len(lret0), pformat(lret0)) ret1 = get_dir_content_old(root_dir, include_folders=False, recursive=True, prepend_folder_name=False) print(len(ret1), pformat(ret1)) if __name__ == "__main__": main()
注意事項:
輸出:
(py35x64_test) E:\\Work\\Dev\\StackOverflow\\q003207219>"e:\\Work\\Dev\\VEnvs\\py35x64_test\\Scripts\\python.exe" "code_os_listdir.py" <generator object get_dir_content at 0x000001BDDBB3DF10> 22 ['root_dir\\\\dir0', 'root_dir\\\\dir0\\\\dir00', 'root_dir\\\\dir0\\\\dir00\\\\dir000', 'root_dir\\\\dir0\\\\dir00\\\\dir000\\\\file0000', 'root_dir\\\\dir0\\\\dir00\\\\file000', 'root_dir\\\\dir0\\\\dir01', 'root_dir\\\\dir0\\\\dir01\\\\file010', 'root_dir\\\\dir0\\\\dir01\\\\file011', 'root_dir\\\\dir0\\\\dir02', 'root_dir\\\\dir0\\\\dir02\\\\dir020', 'root_dir\\\\dir0\\\\dir02\\\\dir020\\\\dir0200', 'root_dir\\\\dir1', 'root_dir\\\\dir1\\\\file10', 'root_dir\\\\dir1\\\\file11', 'root_dir\\\\dir1\\\\file12', 'root_dir\\\\dir2', 'root_dir\\\\dir2\\\\dir20', 'root_dir\\\\dir2\\\\dir20\\\\file200', 'root_dir\\\\dir2\\\\file20', 'root_dir\\\\dir3', 'root_dir\\\\file0', 'root_dir\\\\file1'] 11 ['dir0\\\\dir00\\\\dir000\\\\file0000', 'dir0\\\\dir00\\\\file000', 'dir0\\\\dir01\\\\file010', 'dir0\\\\dir01\\\\file011', 'dir1\\\\file10', 'dir1\\\\file11', 'dir1\\\\file12', 'dir2\\\\dir20\\\\file200', 'dir2\\\\file20', 'file0', 'file1']
[Python 3]:操作系統。scandir ( path='.' ) ( Python 3.5 +, backport: [PyPI]: scandir )
返回與path給出的目錄中的條目對應的os.DirEntry對象的迭代器。 條目以任意順序生成,特殊條目
'.'
和'..'
不包括在內。使用scandir()而不是listdir()可以顯着提高還需要文件類型或文件屬性信息的代碼的性能,因為如果操作系統在掃描目錄時提供了os.DirEntry對象,則會公開此信息。 所有os.DirEntry方法都可能執行系統調用,但is_dir()和is_file()通常只需要對符號鏈接進行系統調用; os.DirEntry.stat()在 Unix 上總是需要一個系統調用,但在 Windows 上只需要一個符號鏈接。
>>> import os >>> root_dir = os.path.join(".", "root_dir") # Explicitly prepending current directory >>> root_dir '.\\\\root_dir' >>> >>> scandir_iterator = os.scandir(root_dir) >>> scandir_iterator <nt.ScandirIterator object at 0x00000268CF4BC140> >>> [item.path for item in scandir_iterator] ['.\\\\root_dir\\\\dir0', '.\\\\root_dir\\\\dir1', '.\\\\root_dir\\\\dir2', '.\\\\root_dir\\\\dir3', '.\\\\root_dir\\\\file0', '.\\\\root_dir\\\\file1'] >>> >>> [item.path for item in scandir_iterator] # Will yield an empty list as it was consumed by previous iteration (automatically performed by the list comprehension) [] >>> >>> scandir_iterator = os.scandir(root_dir) # Reinitialize the generator >>> for item in scandir_iterator : ... if os.path.isfile(item.path): ... print(item.name) ... file0 file1
注意事項:
os.listdir
[Python 3]:操作系統。 walk ( top, topdown=True, onerror=None, followlinks=False )
通過自頂向下或自底向上遍歷樹來生成目錄樹中的文件名。 對於以目錄top 為根的樹中的每個目錄(包括top本身),它產生一個 3 元組(
dirpath
,dirnames
,filenames
)。
>>> import os >>> root_dir = os.path.join(os.getcwd(), "root_dir") # Specify the full path >>> root_dir 'E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir' >>> >>> walk_generator = os.walk(root_dir) >>> root_dir_entry = next(walk_generator) # First entry corresponds to the root dir (passed as an argument) >>> root_dir_entry ('E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir', ['dir0', 'dir1', 'dir2', 'dir3'], ['file0', 'file1']) >>> >>> root_dir_entry[1] + root_dir_entry[2] # Display dirs and files (direct descendants) in a single list ['dir0', 'dir1', 'dir2', 'dir3', 'file0', 'file1'] >>> >>> [os.path.join(root_dir_entry[0], item) for item in root_dir_entry[1] + root_dir_entry[2]] # Display all the entries in the previous list by their full path ['E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir\\\\dir0', 'E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir\\\\dir1', 'E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir\\\\dir2', 'E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir\\\\dir3', 'E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir\\\\file0', 'E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir\\\\file1'] >>> >>> for entry in walk_generator: # Display the rest of the elements (corresponding to every subdir) ... print(entry) ... ('E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir\\\\dir0', ['dir00', 'dir01', 'dir02'], []) ('E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir\\\\dir0\\\\dir00', ['dir000'], ['file000']) ('E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir\\\\dir0\\\\dir00\\\\dir000', [], ['file0000']) ('E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir\\\\dir0\\\\dir01', [], ['file010', 'file011']) ('E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir\\\\dir0\\\\dir02', ['dir020'], []) ('E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir\\\\dir0\\\\dir02\\\\dir020', ['dir0200'], []) ('E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir\\\\dir0\\\\dir02\\\\dir020\\\\dir0200', [], []) ('E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir\\\\dir1', [], ['file10', 'file11', 'file12']) ('E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir\\\\dir2', ['dir20'], ['file20']) ('E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir\\\\dir2\\\\dir20', [], ['file200']) ('E:\\\\Work\\\\Dev\\\\StackOverflow\\\\q003207219\\\\root_dir\\\\dir3', [], [])
注意事項:
os.scandir
(舊版本上的os.listdir
)[Python 3]:glob。 glob ( pathname, *, recursive=False ) ( [Python 3]: glob.iglob ( pathname, *, recursive=False ) )
返回與pathname匹配的可能為空的路徑名列表,它必須是包含路徑規范的字符串。 路徑名可以是絕對的(如
/usr/src/Python-1.5/Makefile
)或相對的(如../../Tools/*/*.gif
),並且可以包含 shell 樣式的通配符。 結果中包含損壞的符號鏈接(如在 shell 中)。
...
在 3.5 版更改: 支持使用“**
”的遞歸全局。
>>> import glob, os >>> wildcard_pattern = "*" >>> root_dir = os.path.join("root_dir", wildcard_pattern) # Match every file/dir name >>> root_dir 'root_dir\\\\*' >>> >>> glob_list = glob.glob(root_dir) >>> glob_list ['root_dir\\\\dir0', 'root_dir\\\\dir1', 'root_dir\\\\dir2', 'root_dir\\\\dir3', 'root_dir\\\\file0', 'root_dir\\\\file1'] >>> >>> [item.replace("root_dir" + os.path.sep, "") for item in glob_list] # Strip the dir name and the path separator from begining ['dir0', 'dir1', 'dir2', 'dir3', 'file0', 'file1'] >>> >>> for entry in glob.iglob(root_dir + "*", recursive=True): ... print(entry) ... root_dir\\ root_dir\\dir0 root_dir\\dir0\\dir00 root_dir\\dir0\\dir00\\dir000 root_dir\\dir0\\dir00\\dir000\\file0000 root_dir\\dir0\\dir00\\file000 root_dir\\dir0\\dir01 root_dir\\dir0\\dir01\\file010 root_dir\\dir0\\dir01\\file011 root_dir\\dir0\\dir02 root_dir\\dir0\\dir02\\dir020 root_dir\\dir0\\dir02\\dir020\\dir0200 root_dir\\dir1 root_dir\\dir1\\file10 root_dir\\dir1\\file11 root_dir\\dir1\\file12 root_dir\\dir2 root_dir\\dir2\\dir20 root_dir\\dir2\\dir20\\file200 root_dir\\dir2\\file20 root_dir\\dir3 root_dir\\file0 root_dir\\file1
注意事項:
os.listdir
[Python 3]:類路徑庫。 路徑( *pathsegments ) ( Python 3.4 +, backport: [PyPI]: pathlib2 )
>>> import pathlib >>> root_dir = "root_dir" >>> root_dir_instance = pathlib.Path(root_dir) >>> root_dir_instance WindowsPath('root_dir') >>> root_dir_instance.name 'root_dir' >>> root_dir_instance.is_dir() True >>> >>> [item.name for item in root_dir_instance.glob("*")] # Wildcard searching for all direct descendants ['dir0', 'dir1', 'dir2', 'dir3', 'file0', 'file1'] >>> >>> [os.path.join(item.parent.name, item.name) for item in root_dir_instance.glob("*") if not item.is_dir()] # Display paths (including parent) for files only ['root_dir\\\\file0', 'root_dir\\\\file1']
注意事項:
[Python 2]:dircache.listdir(path) (僅限Python 2 )
os.listdir
帶有緩存的(薄)包裝器def listdir(path): """List directory contents, using cache.""" try: cached_mtime, list = cache[path] del cache[path] except KeyError: cached_mtime, list = -1, [] mtime = os.stat(path).st_mtime if mtime != cached_mtime: list = os.listdir(path) list.sort() cache[path] = mtime, list return list
[man7]: OPENDIR(3) / [man7]: READDIR(3) / [man7]: CLOSEDIR(3) via [Python 3]: ctypes - Python 的外部函數庫(特定於POSIX )
ctypes是 Python 的外部函數庫。 它提供與 C 兼容的數據類型,並允許調用 DLL 或共享庫中的函數。 它可用於將這些庫包裝在純 Python 中。
code_ctypes.py :
#!/usr/bin/env python3 import sys from ctypes import Structure, \\ c_ulonglong, c_longlong, c_ushort, c_ubyte, c_char, c_int, \\ CDLL, POINTER, \\ create_string_buffer, get_errno, set_errno, cast DT_DIR = 4 DT_REG = 8 char256 = c_char * 256 class LinuxDirent64(Structure): _fields_ = [ ("d_ino", c_ulonglong), ("d_off", c_longlong), ("d_reclen", c_ushort), ("d_type", c_ubyte), ("d_name", char256), ] LinuxDirent64Ptr = POINTER(LinuxDirent64) libc_dll = this_process = CDLL(None, use_errno=True) # ALWAYS set argtypes and restype for functions, otherwise it's UB!!! opendir = libc_dll.opendir readdir = libc_dll.readdir closedir = libc_dll.closedir def get_dir_content(path): ret = [path, list(), list()] dir_stream = opendir(create_string_buffer(path.encode())) if (dir_stream == 0): print("opendir returned NULL (errno: {:d})".format(get_errno())) return ret set_errno(0) dirent_addr = readdir(dir_stream) while dirent_addr: dirent_ptr = cast(dirent_addr, LinuxDirent64Ptr) dirent = dirent_ptr.contents name = dirent.d_name.decode() if dirent.d_type & DT_DIR: if name not in (".", ".."): ret[1].append(name) elif dirent.d_type & DT_REG: ret[2].append(name) dirent_addr = readdir(dir_stream) if get_errno(): print("readdir returned NULL (errno: {:d})".format(get_errno())) closedir(dir_stream) return ret def main(): print("{:s} on {:s}\\n".format(sys.version, sys.platform)) root_dir = "root_dir" entries = get_dir_content(root_dir) print(entries) if __name__ == "__main__": main()
注意事項:
os.walk
的格式返回數據。 我沒有費心讓它遞歸,但從現有代碼開始,這將是一項相當微不足道的任務輸出:
[cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q003207219]> ./code_ctypes.py 3.5.2 (default, Nov 12 2018, 13:43:14) [GCC 5.4.0 20160609] on linux ['root_dir', ['dir2', 'dir1', 'dir3', 'dir0'], ['file1', 'file0']]
[ActiveState.Docs]: win32file.FindFilesW ( Win專用)
使用 Windows Unicode API 檢索匹配文件名的列表。 API FindFirstFileW/FindNextFileW/Find 關閉函數的接口。
>>> import os, win32file, win32con >>> root_dir = "root_dir" >>> wildcard = "*" >>> root_dir_wildcard = os.path.join(root_dir, wildcard) >>> entry_list = win32file.FindFilesW(root_dir_wildcard) >>> len(entry_list) # Don't display the whole content as it's too long 8 >>> [entry[-2] for entry in entry_list] # Only display the entry names ['.', '..', 'dir0', 'dir1', 'dir2', 'dir3', 'file0', 'file1'] >>> >>> [entry[-2] for entry in entry_list if entry[0] & win32con.FILE_ATTRIBUTE_DIRECTORY and entry[-2] not in (".", "..")] # Filter entries and only display dir names (except self and parent) ['dir0', 'dir1', 'dir2', 'dir3'] >>> >>> [os.path.join(root_dir, entry[-2]) for entry in entry_list if entry[0] & (win32con.FILE_ATTRIBUTE_NORMAL | win32con.FILE_ATTRIBUTE_ARCHIVE)] # Only display file "full" names ['root_dir\\\\file0', 'root_dir\\\\file1']
注意事項:
win32file.FindFilesW
是[GitHub] 的一部分:mhammond/pywin32 - Python for Windows (pywin32) Extensions ,它是WINAPI 上的Python包裝器注意事項:
代碼旨在是可移植的(除了針對特定區域的地方 - 已標記)或交叉:
在上述變體中使用了多種路徑樣式(絕對、相對),以說明所使用的“工具”在這個方向上是靈活的
os.listdir
和os.scandir
使用opendir / readdir / closedir ( [MS.Docs]: FindFirstFileW function / [MS.Docs]: FindNextFileW function / [MS.Docs]: FindClose function ) (via [GitHub]: python/cpython - (主) cpython/Modules/posixmodule.c )
win32file.FindFilesW
使用這些( Win特定的)函數(通過[GitHub]: mhammond/pywin32 - (master) pywin32/win32/src/win32file.i )
_get_dir_content (從第 1點開始)可以使用這些方法中的任何一種來實現(有些需要更多的工作,有些需要更少的工作)
filter_func=lambda x: True
(這不會刪除任何內容)和內部_get_dir_content類似: if not filter_func(entry_with_path): continue
(如果函數在一個條目中失敗,它將被跳過),但是代碼變得越復雜,花費的時間就越長執行請注意! 由於使用了遞歸,我必須提到我在我的筆記本電腦 ( Win 10 x64 ) 上做了一些測試,與這個問題完全無關,並且當遞歸級別達到(990 .. 1000)范圍內的某個值時 ( recursionlimit - 1000 (默認)),我得到了StackOverflow :)。 如果目錄樹超過了這個限制(我不是FS專家,所以我不知道這是否可能),那可能是一個問題。
我還必須提到,我沒有嘗試增加recursionlimit,因為我在該領域沒有經驗(在必須增加OS級別的堆棧之前我可以增加多少),但理論上總是有可能失敗,如果目錄深度大於可能的最高遞歸限制(在該機器上)
代碼示例僅用於演示目的。 這意味着我沒有考慮到錯誤處理(我認為沒有任何try / except / else / finally塊),所以代碼並不健壯(原因是:盡可能保持簡單和簡短)。 對於生產,還應添加錯誤處理
僅將Python用作包裝器
我所知道的最著名的風格是我所說的系統管理員方法:
grep
/ findstr
)或輸出格式可以在雙方完成,但我不會堅持這樣做。 另外,我刻意用os.system
代替subprocess.Popen
。(py35x64_test) E:\\Work\\Dev\\StackOverflow\\q003207219>"e:\\Work\\Dev\\VEnvs\\py35x64_test\\Scripts\\python.exe" -c "import os;os.system(\\"dir /b root_dir\\")" dir0 dir1 dir2 dir3 file0 file1
通常應避免這種方法,因為如果某些命令輸出格式在操作系統版本/風格之間略有不同,則解析代碼也應進行調整; 更不用說語言環境之間的差異了)。
我真的很喜歡adamk 的回答,建議您使用同名模塊中的glob()
。 這允許您使用*
進行模式匹配。
但正如其他人在評論中指出的那樣, glob()
可能會因不一致的斜線方向而被絆倒。 為了幫助解決這個問題,我建議您使用os.path
模塊中的join()
和expanduser()
函數,也可能使用os
模塊中的getcwd()
函數。
例如:
from glob import glob
# Return everything under C:\Users\admin that contains a folder called wlp.
glob('C:\Users\admin\*\wlp')
上面的內容很糟糕 - 路徑已被硬編碼,並且只能在驅動器名稱和被硬編碼到路徑中的\\
s 之間的 Windows 上工作。
from glob import glob
from os.path import join
# Return everything under Users, admin, that contains a folder called wlp.
glob(join('Users', 'admin', '*', 'wlp'))
上面的方法效果更好,但它依賴於文件夾名稱Users
,該名稱在 Windows 上經常出現,而在其他操作系統上並不常見。 它還依賴於具有特定名稱admin
的用戶。
from glob import glob
from os.path import expanduser, join
# Return everything under the user directory that contains a folder called wlp.
glob(join(expanduser('~'), '*', 'wlp'))
這在所有平台上都可以完美運行。
另一個很好的例子,它可以完美地跨平台工作並且做一些不同的事情:
from glob import glob
from os import getcwd
from os.path import join
# Return everything under the current directory that contains a folder called wlp.
glob(join(getcwd(), '*', 'wlp'))
希望這些示例可以幫助您了解可以在標准 Python 庫模塊中找到的一些函數的強大功能。
def list_files(path):
# returns a list of names (with extension, without full path) of all files
# in folder path
files = []
for name in os.listdir(path):
if os.path.isfile(os.path.join(path, name)):
files.append(name)
return files
如果您正在尋找find的 Python 實現,這是我經常使用的一個秘訣:
from findtools.find_files import (find_files, Match)
# Recursively find all *.sh files in **/usr/bin**
sh_files_pattern = Match(filetype='f', name='*.sh')
found_files = find_files(path='/usr/bin', match=sh_files_pattern)
for found_file in found_files:
print found_file
所以我用它制作了一個 PyPI包,還有一個GitHub 存儲庫。 我希望有人發現它可能對這段代碼有用。
為了獲得更好的結果,您可以將os
模塊的listdir()
方法與生成器一起使用(生成器是一個強大的迭代器,可以保持其狀態,還記得嗎?)。 以下代碼適用於兩個版本:Python 2 和 Python 3。
這是一個代碼:
import os
def files(path):
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
yield file
for file in files("."):
print (file)
listdir()
方法返回給定目錄的條目列表。 如果給定的條目是文件,則方法os.path.isfile()
返回True
。 並且yield
運算符退出 func 但保持其當前狀態,並且它僅返回檢測為文件的條目的名稱。 以上所有內容都允許我們循環生成器函數。
返回絕對文件路徑列表,不會遞歸到子目錄
L = [os.path.join(os.getcwd(),f) for f in os.listdir('.') if os.path.isfile(os.path.join(os.getcwd(),f))]
一位睿智的老師曾告訴我:
當有幾種既定的方法可以做某事時,沒有一種方法適用於所有情況。
因此,我將為問題的一個子集添加一個解決方案:通常,我們只想檢查文件是否與開始字符串和結束字符串匹配,而無需進入子目錄。 因此,我們想要一個返回文件名列表的函數,例如:
filenames = dir_filter('foo/baz', radical='radical', extension='.txt')
如果您想先聲明兩個函數,可以這樣做:
def file_filter(filename, radical='', extension=''):
"Check if a filename matches a radical and extension"
if not filename:
return False
filename = filename.strip()
return(filename.startswith(radical) and filename.endswith(extension))
def dir_filter(dirname='', radical='', extension=''):
"Filter filenames in directory according to radical and extension"
if not dirname:
dirname = '.'
return [filename for filename in os.listdir(dirname)
if file_filter(filename, radical, extension)]
這個解決方案可以很容易地用正則表達式推廣(如果你不希望你的模式總是粘在文件名的開頭或結尾,你可能想要添加一個pattern
參數)。
import os
import os.path
def get_files(target_dir):
item_list = os.listdir(target_dir)
file_list = list()
for item in item_list:
item_dir = os.path.join(target_dir,item)
if os.path.isdir(item_dir):
file_list += get_files(item_dir)
else:
file_list.append(item_dir)
return file_list
這里我使用遞歸結構。
使用生成器
import os
def get_files(search_path):
for (dirpath, _, filenames) in os.walk(search_path):
for filename in filenames:
yield os.path.join(dirpath, filename)
list_files = get_files('.')
for filename in list_files:
print(filename)
Python 3.4+ 的另一個非常易讀的變體是使用 pathlib.Path.glob:
from pathlib import Path
folder = '/foo'
[f for f in Path(folder).glob('*') if f.is_file()]
更具體的很簡單,例如只在所有子目錄中查找不是符號鏈接的 Python 源文件:
[f for f in Path(folder).glob('**/*.py') if not f.is_symlink()]
對於 Python 2:
pip install rglob
然后做
import rglob
file_list = rglob.rglob("/home/base/dir/", "*")
print file_list
這是我的通用功能。 它返回一個文件路徑列表而不是文件名,因為我發現它更有用。 它有一些可選參數,使其用途廣泛。 例如,我經常將它與pattern='*.txt'
或subfolders=True
。
import os
import fnmatch
def list_paths(folder='.', pattern='*', case_sensitive=False, subfolders=False):
"""Return a list of the file paths matching the pattern in the specified
folder, optionally including files inside subfolders.
"""
match = fnmatch.fnmatchcase if case_sensitive else fnmatch.fnmatch
walked = os.walk(folder) if subfolders else [next(os.walk(folder))]
return [os.path.join(root, f)
for root, dirnames, filenames in walked
for f in filenames if match(f, pattern)]
dircache是“自 2.6 版起已棄用:Python 3.0 中已刪除 dircache 模塊。”
import dircache
list = dircache.listdir(pathname)
i = 0
check = len(list[0])
temp = []
count = len(list)
while count != 0:
if len(list[i]) != check:
temp.append(list[i-1])
check = len(list[i])
else:
i = i + 1
count = count - 1
print temp
我將提供一個示例單行代碼,其中可以提供源路徑和文件類型作為輸入。 該代碼返回帶有 csv 擴展名的文件名列表。 使用. 如果需要返回所有文件。 這也將遞歸掃描子目錄。
[y for x in os.walk(sourcePath) for y in glob(os.path.join(x[0], '*.csv'))]
根據需要修改文件擴展名和源路徑。
從指定的文件夾(包括子目錄)獲取所有文件。
import glob
import os
print([entry for entry in glob.iglob("{}/**".format("DIRECTORY_PATH"), recursive=True) if os.path.isfile(entry) == True])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.