是否有一个内置函数来获取文件对象的字节大小?我看到一些人这样做:
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)
但是根据我对Python的经验,它有很多帮助函数,所以我猜可能有一个内置的。
Tags:
您可以使用
os.stat()
函数,它是系统调用stat()
的包装:试着看看http://docs.python.org/library/os.path.html#os.path.getsize
或者
返回路径的大小(字节)。如果文件不存在或不可访问,则引发os.error。
相关问题 更多 >
编程相关推荐