2012-02-06 68 views
1

我有種寫這段代碼:如何讓下面的一塊代碼工作的

import csv 
import os 
fileobj = csv.reader(open('c:\\paths1.csv', 'rb'), delimiter=' ', quotechar='|') 
for row in fileobj: 
    x=0 

    for x in fileobj: 
     filesize= os.path.getsize(x) 

    print (filesize); 

但是我仍然收到此錯誤:

Traceback (most recent call last): 
    File "C:\Documents and Settings\Administrator\workspace\test1py\Acol.py", line 9, in <module> 
    filesize= os.path.getsize(x) 
    File "C:\Python27\lib\genericpath.py", line 49, in getsize 
    return os.stat(filename).st_size 
TypeError: coercing to Unicode: need string or buffer, list found 

我想這是COS' FileObj文件包含路徑列表...

有沒有人有任何建議?

回答

5

你想

for x in row: # NOT in fileobj 
    filesize = os.path.getsize(x) 

順便說一句,行x = 0有沒有影響,但來迷惑不知情的讀者。你應該刪除它。

+0

非常感謝!驚人!!!對於這個錯誤感到抱歉,但我昨天開始使用python進行編程,我曾經使用其他編程語言編程,需要這樣的東西......無論如何再次感謝 – nassio 2012-02-06 01:07:22

+0

我很高興能夠提供幫助。聽起來這個答案解決了你的問題。如果是這種情況,請考慮[接受它](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。 – phihag 2012-02-06 01:09:42

相關問題