2017-12-13 6 views
-2

하자 내가 특정 폴더에서 파일을 살펴 보는 방법은 무엇입니까?

/home/me/data/picture/ 

및 사진 폴더는 사진 파일의 다양한 포함

폴더를 말한다.
안에있는 모든 파일을 살펴보고 각 파일 이름을 가져 오려면

while file in fileexist: 
    y,sr = somefunction.load("/home/me/data/picture"+file.name); 

특정 폴더의 파일 수는 얼마나 될까요? 그리고 각 파일 이름을 얻으시겠습니까?

+1

'os.listdir ("some_path")'폴더의 파일 목록을 제공합니다. – DavidG

+0

[Python을 사용하여 각 디렉토리의 한 하위 폴더에서 다른 하위 폴더로 파일을 처리하는 방법은 무엇입니까?] (https://stackoverflow.com/questions/29283466/how-to-process-files-from-one-subfolder-to- another-in-each-directory-using-pytho) – zmo

답변

0

또는 사용할 수 있습니다

for file in os.listdir(folder): 
    print(os.path.join(folder, file)) 

또는 특정 디렉토리 내의 모든 경로를 반복하는 os.walk를 사용할 수 있습니다

folder='/home/me/data/picture' 
for path, subdirs, files in os.walk(folder): 
    for file in files: 
     print(os.path.join(path, file)) 

또는 당신이 글로브를 사용할 수 있습니다.

+0

파일 이름 만 가져 오는 방법은 무엇입니까? –

+0

대신'os.path.join (path, file)'을 사용하십시오'file' – zmo

0
from os import listdir 
from os.path import isfile, join 

files_list = [f for f in listdir('your_path/to_folder') if isfile(join('your_path/to_folder', f))] 

print(files_list) 

결과는 특정 폴더에있는 모든 파일을 포함하는 목록입니다

0

당신은 당신이 (os.listdir 사용하는 15.1. os — Miscellaneous operating system interfaces을보고 귀하의 경우 10.1. os.path — Common pathname manipulations

을 할 수 있습니다 path), 지정된 폴더에있는 모든 파일 목록을 반환합니다.

import os 

path = "C:/home/me/data/picture" # full path to folder 

# You can print the number of the file in the folder with 
# the functions len() and os.listdir() 
print(len(os.listdir(path))) 

# You can get the list of the files in the folder with: 
print(os.listdir(path)) 

# if you want a more readable outcome, print each file with for loop: 
for file in os.listdir(path): 
print(file) 

더 작업을 위해, 당신은이 기능에보고 할 수 있습니다 :

os.environ(path)을 홈 디렉토리의 경로 이름에 대해. 현재 디렉토리

os.join(path, *paths)를 들어, 디렉토리

os.getcwd()을 변경

os.chdir(path), 가입 경로

및 MKDIR, makedirs 제거와 같은 많은 다른 사람, 을 removedirs 및 경로 조작에서 그 같은 path.basename, path.dirname, path.exist, path.isfile, path.isdir ...