0
'my_file.txt'라는 파일이 있습니다. 파이썬에서 파일 존재의 정확한 경로를 얻으려면 어떻게해야합니까? 목록으로파이썬에서 파일 경로 가져 오기
예상 출력, ['C:/sample_folder/my_file.txt', 'C:/another_folder/test_folder/my_file.txt']
'my_file.txt'라는 파일이 있습니다. 파이썬에서 파일 존재의 정확한 경로를 얻으려면 어떻게해야합니까? 목록으로파이썬에서 파일 경로 가져 오기
예상 출력, ['C:/sample_folder/my_file.txt', 'C:/another_folder/test_folder/my_file.txt']
당신은 os.walk를 사용할 수 있습니다.
import glob
for path in glob.iglob('/**/README.md', recursive=True):
print(path)
을 그리고 pathlib는 기존의 많은 기능에 객체 지향 인터페이스도있다 :
import os
for root, dirs, files in os.walk('/'):
for name in files:
if name == 'README.md':
path = os.path.join(root, name)
print(path)
도 glob있다
from pathlib import Path
for path in Path('C:/').glob('**/*.txt'):
print(path)
당신이 당신의 전체 하드 드라이브를 검색하려고는? –
파일에 대한 참조가 이미 있습니까? 아니면 현재 디렉토리를 검색하고 있습니까? –
@ JoshLee : 예. 주로 C : \ 드라이브에서 – Sohn