본문 바로가기
파이썬

파이썬 Python에서 파일 크기를 어떻게 확인할 수 있습니까?

by º기록 2020. 12. 26.
반응형

Windows에서 Python 스크립트를 작성하고 있습니다. 파일 크기에 따라 뭔가를하고 싶습니다. 예를 들어, 크기가 0보다 크면 누군가에게 이메일을 보내고 그렇지 않으면 다른 일을 계속합니다.

파일 크기는 어떻게 확인합니까?

 

해결 방법

 


>>> from pathlib import Path
>>> Path('somefile.txt').stat()
os.stat_result(st_mode=33188, st_ino=6419862, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=1564, st_atime=1584299303, st_mtime=1584299400, st_ctime=1584299400)
>>> Path('somefile.txt').stat().st_size
1564


>>> import os
>>> os.stat('somefile.txt')
os.stat_result(st_mode=33188, st_ino=6419862, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=1564, st_atime=1584299303, st_mtime=1584299400, st_ctime=1584299400)
>>> os.stat('somefile.txt').st_size
1564

출력은 바이트 단위입니다.

 

참조 페이지 https://stackoverflow.com/questions/2104080

 

 

반응형

댓글