-1
import os

searchFolder = '/Users/bubble/Desktop/pics'


for root, dirs, files in os.walk(searchFolder):
    for file in files:
        pathName = os.path.join(root,file)
        print pathName
        print os.path.getsize(pathName)
        print 
        f = open('/Users/bubble/Desktop/workfile.txt','w')
        f.write(pathName) 

只有一行被打印到文本文件:/Users/bubble/Desktop/pics/IMG_2999.JPG

我希望将整个输出打印到一个文本文件中(也许稍后,甚至是一个我可以编辑的 html 文件看起来很漂亮)

谢谢

4

1 回答 1

2

您正在循环覆盖文件。打开一次。

import os

searchFolder = '/Users/bubble/Desktop/pics'

with open('/Users/bubble/Desktop/workfile.txt','w') as f:
    for root, dirs, files in os.walk(searchFolder):
        for file in files:
            pathName = os.path.join(root,file)
            print pathName
            print os.path.getsize(pathName)
            print
            f.write('{}\n'.format(pathName))
            f.write('{}\n'.format(os.path.getsize(pathName)))
于 2013-07-22T06:54:03.333 回答