Divide and Conquer

[Python] 절대 경로 파일 위치 찾기 본문

성장캐/기타

[Python] 절대 경로 파일 위치 찾기

10살 2022. 5. 14. 21:05
728x90
import time
import math
import sys, os
start = time.time()
def findfile(name, path):
    for dirpath, dirname, filename in os.walk(path):
        if name in filename:
            return os.path.join(dirpath, name)
filepath = findfile("bm0514.html", "/")
print(filepath)
end = time.time()
print(f"파일찾기 시간 {end - start:.5f} sec")
/Users\012vi\Desktop\bm0514.html
파일찾기 시간 10.82805 sec
start = time.time()
a = os.path.abspath('bm0514.html') 
print(a)
end = time.time()
print(f"파일찾기 시간 {end - start:.5f} sec")
C:\Users\012vi\Desktop\bm0514.html
파일찾기 시간 0.00000 sec

결론: <<< 틀렸다 findfile 써야 한다 아래 코드는 위치와 실행 코드가 같이 있을 때만 된다

import os
os.path.abspath('bm0514.html').replace('\\', '/')
'C:/Users/012vi/Desktop/bm0514.html'
반응형
Comments