본문 바로가기

카테고리 없음

[python] os 파일정보

반응형
파일 기본
import os

print(os.getcwd()) # current working directory 현재 작업공간

os.chdir("코딩공부") # "코딩공부 로 작업공간 이동
print(os.getcwd())

os.chdir("..") # 상위폴더로 이동
print(os.getcwd())

os.chdir("../..") # 상위의 상위폴더로 이동
print(os.getcwd())

os.chdir("c:/") 주어진 절대경로로 이동

 

파일경로
import os

#파일경로 만들기
file_path = os.path.join(os.getcwd(), "f_file.txt") #w절대경로 생성
print(file_path)

#파일경로에서 폴더정보 가져오기
print(os.path.dirname(r"c:\users\workspace\f_file.txt"))

 

파일정보 가져오기
#파일 정보 가져오기

import time
import datetime

#파일의 생성날짜
ctime = os.path.getctime("경로지정")
print(ctime)

#날짜정보를 strftime을 통해서 연우러일 시분초 형태로 출력
print(datetime.datetime.fromtimestamp(ctime).strftime("%Y%m%d %H:%M:%S"))


#파일의 수정 날짜
ctime = os.path.getmtime("경로지정")
print(datetime.datetime.fromtimestamp(mtime).strftime("%Y%m%d %H:%M:%S"))


#파일의 마지막 접근 날짜
atime = os.path.getatime("경로지정")
print(datetime.datetime.fromtimestamp(atime).strftime("%Y%m%d %H:%M:%S"))


#파일크기
size = os.path.getsize("경로지정")
print(size)

 

출처 : 나도코딩

반응형