티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

How do I check file size in Python?

파이썬에서 파일 크기 확인하기

 문제 내용 

How do I get the size of a file in Python?

파이썬에서 파일 크기를 얻으려면 어떻게 해야 합니까?

 

 

 

 높은 점수를 받은 Solution 

Use os.path.getsize:

os.path.getsize 사용:
>>> import os
>>> os.path.getsize("/path/to/file.mp3")
2071611

 

The output is in bytes.

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

 

 

 

 가장 최근 달린 Solution 

we have two options Both include importing os module

두 가지 옵션이 있습니다. 둘 다 OS 모듈 가져오기를 포함합니다.

 

1)

import os
os.stat("/path/to/file").st_size

as os.stat() function returns an object which contains so many headers including file created time and last modified time etc.. among them st_size gives the exact size of the file. File path can be either absolute or relative.

os.stat() 함수는 파일 생성 시간 및 마지막으로 수정된 시간 등을 포함하여 매우 많은 헤더를 포함하는 개체를 반환합니다. 그 중 st_size는 파일의 정확한 크기를 제공합니다. 파일 경로는 절대 경로 또는 상대 경로일 수 있습니다.

 

2) In this, we have to provide the exact file path, File path can be either relative or absolute.

2) 여기서, 우리는 정확한 파일 경로를 제공해야 한다. 파일 경로는 상대적이거나 절대적일 수 있다.
import os
os.path.getsize("path of file")

 

 

출처 : https://stackoverflow.com/questions/2104080/how-do-i-check-file-size-in-python

반응형
댓글
공지사항
최근에 올라온 글