import os
for root, dirs, files in os.walk(dir):for f in files:if os.path.splitext(f)[1] == '.txt':fullpath = os.path.join(root, f)print(fullpath)
或者使用生成器:
import os
fileiter = (os.path.join(root, f)for root, _, files in os.walk(dir)for f in files)txtfileiter = (f for f in fileiter if os.path.splitext(f)[1] == '.txt')for txt in txtfileiter:print(txt)
import osfnames = ([file for root, dirs, files in os.walk(dir)for file in filesif file.endswith('.txt') #or file.endswith('.png') or file.endswith('.pdf')])for fname in fnames: print(fname)
from fnmatch import filterfrom functools import partialfrom itertools import chainfrom os import path, walk
print(*chain(*(map(partial(path.join, root), filter(filenames, "*.txt")) for root, _, filenames in walk("mydir"))))
import glob, osos.chdir("H:\\wallpaper")# use whatever directory you want
#double\\ no single \
for file in glob.glob("**/*.txt", recursive = True):print(file)
import os
# This is the path where you want to searchpath = r'd:'
# this is extension you want to detectextension = '.txt' # this can be : .jpg .png .xls .log .....
for root, dirs_list, files_list in os.walk(path):for file_name in files_list:if os.path.splitext(file_name)[-1] == extension:file_name_path = os.path.join(root, file_name)print file_nameprint file_name_path # This is the full path of the filter file
def get_all_filepaths(root_path, ext):"""Search all files which have a given extension within root_path.
This ignores the case of the extension and searches subdirectories, too.
Parameters----------root_path : strext : str
Returns-------list of str
Examples-------->>> get_all_filepaths('/run', '.lock')['/run/unattended-upgrades.lock','/run/mlocate.daily.lock','/run/xtables.lock','/run/mysqld/mysqld.sock.lock','/run/postgresql/.s.PGSQL.5432.lock','/run/network/.ifstate.lock','/run/lock/asound.state.lock']"""import osall_files = []for root, dirs, files in os.walk(root_path):for filename in files:if filename.lower().endswith(ext):all_files.append(os.path.join(root, filename))return all_files
您还可以使用yield创建生成器,从而避免组装完整列表:
def get_all_filepaths(root_path, ext):import osfor root, dirs, files in os.walk(root_path):for filename in files:if filename.lower().endswith(ext):yield os.path.join(root, filename)
import os
def files_in_dir(path, extension=''):"""Generator: yields all of the files in <path> ending with<extension>
\param path Absolute or relative path to inspect,\param extension [optional] Only yield files matching this,
\yield [filenames]"""
for _, dirs, files in os.walk(path):dirs[:] = [] # do not recurse directories.yield from [f for f in files if f.endswith(extension)]
# Example: print all the .py files in './python'for filename in files_in_dir('./python', '*.py'):print("-", filename)
或者对于一个你不需要发电机的地方:
path, ext = "./python", ext = ".py"for _, _, dirfiles in os.walk(path):matches = (f for f in dirfiles if f.endswith(ext))break
for filename in matches:print("-", filename)
如果您要将匹配用于其他内容,您可能希望将其设为列表而不是生成器表达式:
matches = [f for f in dirfiles if f.endswith(ext)]
import osimport pathlibimport timeitimport glob
def a():path = pathlib.Path().cwd()list_sqlite_files = [str(f) for f in path.glob("*.sqlite")]
def b():path = os.getcwd()list_sqlite_files = [f.path for f in os.scandir(path) if os.path.splitext(f)[1] == ".sqlite"]
def c():path = os.getcwd()list_sqlite_files = [os.path.join(path, f) for f in os.listdir(path) if f.endswith(".sqlite")]
def d():path = os.getcwd()os.chdir(path)list_sqlite_files = [os.path.join(path, f) for f in glob.glob("*.sqlite")]
def e():path = os.getcwd()list_sqlite_files = [os.path.join(path, f) for f in glob.glob1(str(path), "*.sqlite")]
def f():path = os.getcwd()list_sqlite_files = []for root, dirs, files in os.walk(path):for file in files:if file.endswith(".sqlite"):list_sqlite_files.append( os.path.join(root, file) )break
print(timeit.timeit(a, number=1000))print(timeit.timeit(b, number=1000))print(timeit.timeit(c, number=1000))print(timeit.timeit(d, number=1000))print(timeit.timeit(e, number=1000))print(timeit.timeit(f, number=1000))
from os import listdirfrom os.path import isfile, joinpath = "/dataPath/"onlyTxtFiles = [f for f in listdir(path) if isfile(join(path, f)) and f.endswith(".txt")]print onlyTxtFiles
import osimport reimport pandas as pdimport numpy as np
def findFilesInFolderYield(path, extension, containsTxt='', subFolders = True, excludeText = ''):""" Recursive function to find all files of an extension type in a folder (and optionally in all subfolders too)
path: Base directory to find filesextension: File extension to find. e.g. 'txt'. Regular expression. Or 'ls\d' to match ls1, ls2, ls3 etccontainsTxt: List of Strings, only finds file if it contains this text. Ignore if '' (or blank)subFolders: Bool. If True, find files in all subfolders under path. If False, only searches files in the specified folderexcludeText: Text string. Ignore if ''. Will exclude if text string is in path."""if type(containsTxt) == str: # if a string and not in a listcontainsTxt = [containsTxt]
myregexobj = re.compile('\.' + extension + '$') # Makes sure the file extension is at the end and is preceded by a .
try: # Trapping a OSError or FileNotFoundError: File permissions problem I believefor entry in os.scandir(path):if entry.is_file() and myregexobj.search(entry.path): #
bools = [True for txt in containsTxt if txt in entry.path and (excludeText == '' or excludeText not in entry.path)]
if len(bools)== len(containsTxt):yield entry.stat().st_size, entry.stat().st_atime_ns, entry.stat().st_mtime_ns, entry.stat().st_ctime_ns, entry.path
elif entry.is_dir() and subFolders: # if its a directory, then repeat process as a nested functionyield from findFilesInFolderYield(entry.path, extension, containsTxt, subFolders)except OSError as ose:print('Cannot access ' + path +'. Probably a permissions error ', ose)except FileNotFoundError as fnf:print(path +' not found ', fnf)
def findFilesInFolderYieldandGetDf(path, extension, containsTxt, subFolders = True, excludeText = ''):""" Converts returned data from findFilesInFolderYield and creates and Pandas Dataframe.Recursive function to find all files of an extension type in a folder (and optionally in all subfolders too)
path: Base directory to find filesextension: File extension to find. e.g. 'txt'. Regular expression. Or 'ls\d' to match ls1, ls2, ls3 etccontainsTxt: List of Strings, only finds file if it contains this text. Ignore if '' (or blank)subFolders: Bool. If True, find files in all subfolders under path. If False, only searches files in the specified folderexcludeText: Text string. Ignore if ''. Will exclude if text string is in path."""
fileSizes, accessTimes, modificationTimes, creationTimes , paths = zip(*findFilesInFolderYield(path, extension, containsTxt, subFolders))df = pd.DataFrame({'FLS_File_Size':fileSizes,'FLS_File_Access_Date':accessTimes,'FLS_File_Modification_Date':np.array(modificationTimes).astype('timedelta64[ns]'),'FLS_File_Creation_Date':creationTimes,'FLS_File_PathName':paths,})
df['FLS_File_Modification_Date'] = pd.to_datetime(df['FLS_File_Modification_Date'],infer_datetime_format=True)df['FLS_File_Creation_Date'] = pd.to_datetime(df['FLS_File_Creation_Date'],infer_datetime_format=True)df['FLS_File_Access_Date'] = pd.to_datetime(df['FLS_File_Access_Date'],infer_datetime_format=True)
return df
ext = 'txt' # regular expressioncontainsTxt=[]path = 'C:\myFolder'df = findFilesInFolderYieldandGetDf(path, ext, containsTxt, subFolders = True)