2011-07-14 24 views
-1

如何在Python中獲取.mov文件的大小。與其他文件,我可以這樣做:在Python中獲取電影文件的大小

>>> 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 

是什麼原因造成這個錯誤,以及如何將我得到的文件大小?

+0

呢'os.path.getsize( 「」)'工作? – Jacob

+0

使用文件對象,你可以尋找到文件的末尾,並使用'tell()'來獲得當前位置(即長度)。雖然我不知道這有多可靠。 –

+0

爲什麼不檢查os.path.getsize()的文檔以找出它需要的文件名? –

回答

6

你做錯了。 os.path.getsize需要一個文件名,而不是文件內容。我不知道爲什麼你的第一個代碼示例工作。因此您只需致電os.path.getsize(<file>)即可。

+1

probopably,因爲該文件的內容是一個有效的文件名:) – keppla

4

嘗試以下操作:

os.path.getsize(FILENAME_AS_STRING)