나는 이러한 작업을 수행하는 몇 가지 방법이 있습니다. 솔루션의 간결한 특성에 대해서는 언급 할 수 없습니다. FWIW는 여기에 있습니다 :
1. 아래 코드는 .txt로 끝나는 모든 파일을 사용합니다. 당신은 경로가 함수에 제공되는 것을 가정합니다 여기에 ".endswith"부분을
import os
for root, dirs, files in os.walk('./'): #current directory in terminal
for file in files:
if file.endswith('.txt'):
#here you can do whatever you want to with the file.
2.이 코드를 제거 할 수 있으며, 목록에있는 모든 .txt 인 파일을 추가하고 하위 디렉토리는에있는 경우 경로는 하위 디렉토리에서 해당 파일 내가 코드가 본질적으로 단지 골격 알고하지만 난 당신이 일반적으로 사진을 얻을 수 있다고 생각
def readFilesNameList(self, path):
basePath = path
allfiles = []
subfiles = []
for root, dirs, files in os.walk(basePath):
for f in files:
if f.endswith('.txt'):
allfiles.append(os.path.join(root,f))
if root!=basePath:
subfiles.append(os.path.join(root, f))
를 서브 파일을 추가합니다. 간결한 방법을 찾으면
게시물을 게시하십시오! :)