python -- file, 폴더 다루기
python -- file, 폴더 확인하기
환경 : windows 10 64bit, python 3.6 32bit
<< 지정한 폴더내부의 file, 디렉토리 목록 구하기 >>
>>> import os
>>> mypath = "C:/eBEST"
>>>
>>> print(os.listdir(mypath))
['Common', 'eBestPro', 'speddownlist.ini', 'Temp', 'xingAPI']
>>>
>>> for x in os.listdir(mypath):
if os.path.isfile(os.path.join(mypath, x)):
print("file ==> {0}".format(x))
if os.path.isdir(os.path.join(mypath, x)):
print("folder ==> {0}".format(x))
folder ==> Common
folder ==> eBestPro
file ==> speddownlist.ini
folder ==> Temp
folder ==> xingAPI
>>>
>>>
<< 폴더 경로구분자 '/', '\' 상광없이 인식하여, 경로포함 내부파일목록 가져오기 >>
** os.path.abspath() 를 사용한다.
<< os.sep 사용하기 >>
os.sep -- OS 에 상관없이 디렉토리 구분자 역할함.
>>> aa = "c:/stockdata2/"
>>>
>>> os.path.abspath(aa)
>>>
>>> os.sep
>>>
>>> os.path.abspath(aa) + os.sep + 'aa.txt'
<< 현재 실행파일의 path 알기 >>
dir = os.path.dirname(__file__)
print(dir)