Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取Python中电影文件的大小_Python - Fatal编程技术网

获取Python中电影文件的大小

获取Python中电影文件的大小,python,Python,如何获得Python中.mov文件的大小。对于其他文件,我可以执行以下操作: >>> f = open('<file>') >>> import os >>> os.path.getsize(f.read()) >>f=open(“”) >>>导入操作系统 >>>getsize(f.read()) 但是,当我尝试使用.mov文件执行此操作时,会出现以下错误: >>> os.path.getsize(f.rea

如何获得Python中.mov文件的大小。对于其他文件,我可以执行以下操作:

>>> f = open('<file>')
>>> import os
>>> os.path.getsize(f.read())
>>f=open(“”)
>>>导入操作系统
>>>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
>os.path.getsize(f.read())
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
getsize中的文件“/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/genericpath.py”,第49行
返回os.stat(文件名).st\u大小
TypeError:stat()参数1必须是(不带空字节的编码字符串),而不是str

导致此错误的原因是什么?如何获取文件大小?

您做错了
os.path.getsize
采用文件名,而不是文件内容。我不知道为什么您的第一个代码示例可以工作

因此,您只需调用
os.path.getsize()

尝试以下操作:

os.path.getsize(FILENAME_AS_STRING)

os.path.getsize(“”
有效吗?对于文件对象,您可以查找文件的末尾,并使用
tell()
获取当前位置(即长度)。虽然我不知道这有多可靠。为什么不检查os.path.getsize()的文档以确定它需要一个文件名?可能是因为文件的内容是一个有效的文件名:)