Programming

파이썬에서 파일 크기를 얻으십니까?

procodes 2020. 2. 19. 22:17
반응형

파이썬에서 파일 크기를 얻으십니까?


이 질문에는 이미 답변이 있습니다.

파일 객체의 크기를 바이트 단위로 가져 오는 내장 함수가 있습니까? 어떤 사람들은 다음과 같은 일을합니다.

def getSize(fileobject):
    fileobject.seek(0,2) # move the cursor to the end of the file
    size = fileobject.tell()
    return size

file = open('myfile.bin', 'rb')
print getSize(file)

그러나 파이썬에 대한 경험을 통해 많은 도우미 함수가 있으므로 내장 함수가 하나 있다고 생각합니다.


http://docs.python.org/library/os.path.html#os.path.getsize를 살펴보십시오.

os.path.getsize (경로) 경로의 크기를 바이트 단위로 반환합니다. 파일이 없거나 액세스 할 수없는 경우 os.error를 발생 시키십시오.

import os
os.path.getsize('C:\\Python27\\Lib\\genericpath.py')

또는

os.stat('C:\\Python27\\Lib\\genericpath.py').st_size 

os.path.getsize(path)

경로의 크기를 바이트 단위로 반환합니다. 파일이 없거나 액세스 할 수없는 경우 os.error를 발생 시키십시오.


os.stat()시스템 호출 래퍼 인 function을 사용할 수 있습니다 stat().

import os

def getSize(filename):
    st = os.stat(filename)
    return st.st_size

시험

os.path.getsize(filename)

os.stat ()에 의해보고 된 파일의 크기를 반환해야합니다.


당신은 os.stat(path)전화 를 사용할 수 있습니다

http://docs.python.org/library/os.html#os.stat

참고 URL : https://stackoverflow.com/questions/6591931/getting-file-size-in-python



반응형