这个问题是关于获取位于硬盘内的多个文件的大小。代码已根据此处的答案进行了改进:
import os, glob
with open ('file_size.txt', 'w') as outfile:
files= glob.glob ('*.jpg')
for file in files:
get_sizes = os.path.getsize (file)
for item in get_sizes:
if item < 1000:
size = str (item) + 'K'
else:
size = str (round ((item/(1024*1024)),1)) + 'M'
file_name = os.path.basename (file)
outfile.write(file_name + ' ' + size + '\n')
但是上面的代码仍然会产生以下错误:
Traceback (most recent call last):
File "G:\test\test.py", line 6, in <module>
for item in get_sizes:
TypeError: 'int' object is not iterable
怎么了?