답변:
당신은 사용할 수 있습니다 glob:
import glob, os
os.chdir("/mydir")
for file in glob.glob("*.txt"):
print(file)
또는 간단히 os.listdir:
import os
for file in os.listdir("/mydir"):
if file.endswith(".txt"):
print(os.path.join("/mydir", file))
또는 디렉토리를 탐색하려면 os.walk다음을 사용하십시오 .
import os
for root, dirs, files in os.walk("/mydir"):
for file in files:
if file.endswith(".txt"):
print(os.path.join(root, file))
for file in f보다 더 적절할 것이라고 생각합니다 for files in f. 더 좋은 점은을 변경하는 것 f에 files다음 루프에 대한이 될 수 있고 for file in files.
file. 예약어가 아니며 미리 정의 된 함수의 이름 일 뿐이므로 고유 코드에서 변수 이름으로 사용할 수 있습니다. 일반적으로 그러한 충돌을 피해야한다는 것은 사실이지만, 그것을 사용해야 file할 필요가 거의 없기 때문에 특별한 경우입니다. 따라서 종종 지침의 예외로 간주됩니다. 그렇게하지 않으려면 PEP8은 이러한 이름에 단일 밑줄을 추가하는 것이 좋습니다. 즉 file_, 동의해야 할 내용은 여전히 읽을 수 있습니다.
glob을 사용하십시오 .
>>> import glob
>>> glob.glob('./*.txt')
['./outline.txt', './pip-log.txt', './test.txt', './testingvim.txt']
그와 같은 일이 일을해야합니다
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.txt'):
print file
root, dirs, files대신 변수 이름을 지정하려면 +1 입니다 r, d, f. 훨씬 더 읽기 쉽습니다.
이와 같은 것이 효과가 있습니다.
>>> import os
>>> path = '/usr/share/cups/charmaps'
>>> text_files = [f for f in os.listdir(path) if f.endswith('.txt')]
>>> text_files
['euc-cn.txt', 'euc-jp.txt', 'euc-kr.txt', 'euc-tw.txt', ... 'windows-950.txt']
os.path.join각 요소에 사용할 수 있습니다 text_files. 같은 것일 수 있습니다 text_files = [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.txt')].
당신은 단순히 pathlibs 1을 사용할 수 있습니다 :glob
import pathlib
list(pathlib.Path('your_directory').glob('*.txt'))
또는 루프에서 :
for txt_file in pathlib.Path('your_directory').glob('*.txt'):
# do something with "txt_file"
재귀를 원한다면 사용할 수 있습니다. .glob('**/*.txt)
1pathlib 모듈 파이썬 3.4 표준 라이브러리에 포함시켰다. 그러나 당신은 오래된 파이썬 버전 (즉, 사용에 해당 모듈의-포트를 다시 설치할 수 있습니다 conda또는 pip:) pathlib및 pathlib2.
**/*.txt이전 파이썬 버전에서는 지원되지 않으므로 다음과 같이 해결했습니다. foundfiles= subprocess.check_output("ls **/*.txt", shell=True) for foundfile in foundfiles.splitlines(): print foundfile
pathlib수있는 일에 대한 쇼케이스였으며 이미 Python 버전 요구 사항을 포함 시켰습니다. :) 그러나 귀하의 접근법이 아직 게시되지 않았다면 다른 답변으로 추가하지 않는 이유는 무엇입니까?
rglob항목을 재귀 적으로 찾으려면 사용할 수도 있습니다 . 예.rglob('*.txt')
import os
path = 'mypath/path'
files = os.listdir(path)
files_txt = [i for i in files if i.endswith('.txt')]
나는 os.walk ()를 좋아 한다 :
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 glob
for f in glob.iglob("/mydir/*/*.txt"): # generator, search immediate subdirectories
print f
print glob.glob1("/mydir", "*.tx?") # literal_directory, basename_pattern
import fnmatch, os
print fnmatch.filter(os.listdir("/mydir"), "*.tx?") # include dot-files
glob1()의 도우미 함수입니다 glob. 소스 파일에서 수행하는 작업을 설명하는 인라인 주석이 있습니다 (참조) .../Lib/glob.py.
glob.glob1()공개는 아니지만 Python 2.4-2.7; 3.0-3.2에서 사용할 수 있습니다. 파이 jython github.com/zed/test_glob1
glob모듈 에서 쉽게 추출 할 수있는 것처럼 보입니다 .
path.py는 또 다른 대안입니다 : https://github.com/jaraco/path.py
from path import path
p = path('/path/to/the/directory')
for f in p.files(pattern='*.txt'):
print f
for f in p.walk(pattern='*.txt')모든 하위 폴더를 사용하고 있습니다
list(p.glob('**/*.py'))
재귀 함수에서 os.scandir을 사용하는 빠른 방법. 폴더 및 하위 폴더에서 지정된 확장자를 가진 모든 파일을 검색합니다.
import os
def findFilesInFolder(path, pathList, extension, subFolders = True):
""" Recursive function to find all files of an extension type in a folder (and optionally in all subfolders too)
path: Base directory to find files
pathList: A list that stores all paths
extension: File extension to find
subFolders: Bool. If True, find files in all subfolders under path. If False, only searches files in the specified folder
"""
try: # Trapping a OSError: File permissions problem I believe
for entry in os.scandir(path):
if entry.is_file() and entry.path.endswith(extension):
pathList.append(entry.path)
elif entry.is_dir() and subFolders: # if its a directory, then repeat process as a nested function
pathList = findFilesInFolder(entry.path, pathList, extension, subFolders)
except OSError:
print('Cannot access ' + path +'. Probably a permissions error')
return pathList
dir_name = r'J:\myDirectory'
extension = ".txt"
pathList = []
pathList = findFilesInFolder(dir_name, pathList, extension, True)
10,000s 파일이 포함 된 디렉토리를 검색하는 경우 목록에 추가하는 것이 비효율적입니다. 결과를 '수확'하는 것이 더 나은 솔루션입니다. 또한 출력을 Pandas Dataframe으로 변환하는 기능도 포함했습니다.
import os
import re
import pandas as pd
import 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 files
extension: File extension to find. e.g. 'txt'. Regular expression. Or 'ls\d' to match ls1, ls2, ls3 etc
containsTxt: 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 folder
excludeText: Text string. Ignore if ''. Will exclude if text string is in path.
"""
if type(containsTxt) == str: # if a string and not in a list
containsTxt = [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 believe
for 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 function
yield 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 files
extension: File extension to find. e.g. 'txt'. Regular expression. Or 'ls\d' to match ls1, ls2, ls3 etc
containsTxt: 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 folder
excludeText: 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 expression
containsTxt=[]
path = 'C:\myFolder'
df = findFilesInFolderYieldandGetDf(path, ext, containsTxt, subFolders = True)
이것을 시도하면 모든 파일을 재귀 적으로 찾을 수 있습니다.
import glob, os
os.chdir("H:\\wallpaper")# use whatever directory you want
#double\\ no single \
for file in glob.glob("**/*.txt", recursive = True):
print(file)
**. 파이썬 3에서만 사용할 수 있습니다. 내가 싫어하는 것은 chdir부분입니다. 그럴 필요가 없습니다.
filepath = os.path.join('wallpaper')다음로 사용 glob.glob(filepath+"**/*.psd", recursive = True)하여 동일한 결과를 얻을 수 있습니다.
테스트 (Python 3.6.4, W7x64)를 수행하여 특정 확장자를 가진 파일의 전체 파일 경로 목록을 가져 오기 위해 하위 디렉토리가 아닌 하나의 폴더에 가장 빠른 솔루션을 확인했습니다.
요약하자면,이 작업 os.listdir()이 가장 빠르며 다음 최고 속도보다 1.7 배 빠릅니다 os.walk()(중단됨!), 2.7 pathlib배 빠름, 3.2 배 빠름 os.scandir()및 3.3 배 빠름 glob.
재귀 적 결과가 필요할 때 이러한 결과가 변경 될 수 있습니다. 아래의 방법 중 하나를 복사 / 붙여 넣는 경우 .lower ()를 추가하십시오. 그렇지 않으면 .ext를 검색 할 때 .EXT를 찾을 수 없습니다.
import os
import pathlib
import timeit
import 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))
결과 :
# Python 3.6.4
0.431
0.515
0.161
0.548
0.537
0.274
fnmatch 사용 : https://docs.python.org/2/library/fnmatch.html
import fnmatch
import os
for file in os.listdir('.'):
if fnmatch.fnmatch(file, '*.txt'):
print file
하위 디렉토리가있는 기능적 솔루션 :
from fnmatch import filter
from functools import partial
from itertools import chain
from os import path, walk
print(*chain(*(map(partial(path.join, root), filter(filenames, "*.txt")) for root, _, filenames in walk("mydir"))))
폴더에 많은 파일이 있거나 메모리가 제한적인 경우 생성기를 사용하는 것이 좋습니다.
def yield_files_with_extensions(folder_path, file_extension):
for _, _, files in os.walk(folder_path):
for file in files:
if file.endswith(file_extension):
yield file
옵션 A : 반복
for f in yield_files_with_extensions('.', '.txt'):
print(f)
옵션 B : 모두 가져 오기
files = [f for f in yield_files_with_extensions('.', '.txt')]
고스트 독과 비슷한 복사 가능 솔루션 :
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 : str
ext : 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 os
all_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
Python OS 모듈을 사용 하여 특정 확장자를 가진 파일을 찾으십시오.
간단한 예는 다음과 같습니다.
import os
# This is the path where you want to search
path = r'd:'
# this is extension you want to detect
extension = '.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_name
print file_name_path # This is the full path of the filter file
많은 사용자가 os.walk모든 파일뿐만 아니라 모든 디렉토리와 하위 디렉토리 및 해당 파일을 포함 하는 답변 으로 답변했습니다.
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)]
for루프 를 사용하여 간단한 방법 :
import os
dir = ["e","x","e"]
p = os.listdir('E:') #path
for n in range(len(p)):
name = p[n]
myfile = [name[-3],name[-2],name[-1]] #for .txt
if myfile == dir :
print(name)
else:
print("nops")
이것은 좀 더 일반화 될 수 있습니다.