|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#计算当前文件夹下的所有文件大小
import os
all_files=os.listdir('E:/')
dict1=dict()
list_name=[]
list_ext=[]
list_size=[]
for each_file in all_files:
if os.path.isdir(each_file):
continue
else:
#分别记录文件名,类型,文件大小
name=os.path.splitext(each_file)[0]
ext=os.path.splitext(each_file)[1]
size=os.path.getsize(each_file)
list_name.append(name)
list_ext.append(ext)
list_size.append(size)
for i in range(len(list_name)):
print('%s%s%s'%(list_name[i],list_ext[i],list_size[i]))
出现错误为Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\question.py", line 15, in <module>
size=os.path.getsize(each_file)
File "F:\lib\genericpath.py", line 50, in getsize
return os.stat(filename).st_size
FileNotFoundError: [WinError 2] 系统找不到指定的文件。: '$RECYCLE.BIN'
- import os
- all_files=os.listdir('E:/')
- dict1=dict()
- list_name=[]
- list_ext=[]
- list_size=[]
- for each_file in all_files:
- each_file="E:/"+each_file #全路径啊
- if os.path.isdir(each_file):
- #print(each_file)
- continue
- else:
- #分别记录文件名,类型,文件大小
- name=os.path.splitext(each_file)[0]
- ext=os.path.splitext(each_file)[1]
- size=os.path.getsize(each_file)
- list_name.append(name)
- list_ext.append(ext)
- list_size.append(size)
-
- for i in range(len(list_name)):
- print('%s"\t"%s"\t"%s'%(list_name[i],list_ext[i],list_size[i]))
复制代码
|
|