如何使下面的代码工作

时间:2012-02-06 00:44:32

标签: python python-2.7

我写了这段代码:

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包含路径列表......

有人有任何建议吗?

1 个答案:

答案 0 :(得分:5)

你想要

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

顺便说一句,x = 0行没有任何效果,只会让毫无戒心的读者感到困惑。你应该删除它。