在Python中获取电影文件大小
我想知道怎么用Python获取一个.mov文件的大小。对于其他类型的文件,我可以这样做:
>>> f = open('<file>')
>>> import os
>>> os.path.getsize(f.read())
但是,当我尝试对一个.mov文件这样做时,我遇到了以下错误:
>>> os.path.getsize(f.read())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/genericpath.py", line 49, in getsize
return os.stat(filename).st_size
TypeError: stat() argument 1 must be (encoded string without NULL bytes), not str
这个错误是怎么回事,我该怎么获取文件大小呢?
2 个回答
4
试试下面这个:
os.path.getsize(FILENAME_AS_STRING)
6
你这样做是不对的。os.path.getsize
这个函数需要的是文件名,而不是文件的内容。我也不知道你第一个代码示例为什么能运行。
所以你只需要调用 os.path.getsize(<file>)
这个函数就可以了。