这怎么能写成better/simpler?
how could this be writtern better/simpler?
所以目标是在目录树中找到文件数量最多的文件夹
以及使用最多磁盘的文件夹 space.
import os
path = 'C:\animal'
highestsize = ''
mostfile = ''
totalsize = 0
totalfile = 0
for i in os.listdir(path):
if not os.path.isfile(path + '\' + i):
count = 0
count_file = 0
for dirpath, subfolder, filenames in os.walk(path + '\' + i):
for file in filenames:
count_file += 1
count += os.path.getsize(os.path.join(dirpath, file))
if count_file > totalfile:
totalfile = count_file
mostfile = i
if count > totalsize:
totalsize = count
highestsize = i
print(highestsize, str(totalsize) + ' Byte')
print(mostfile + ' have the most files: ' + str(totalfile))
我设置了一个包含子文件夹和文件的虚拟文件夹,如下所示。
### Root folder with 3 sub folders, each with some files.
# root/
# sub1/
# sub2/
# sub3/
现在您可以试试这个来获取文件数和文件夹大小 -
path = '/Users/akshay/Desktop/root'
folder_file_counts = {r:len([r+'/'+files for files in f]) for r,d,f in os.walk(path)}
folder_sizes_bytes = {r:sum([os.stat(r+'/'+files).st_size for files in f]) for r,d,f in os.walk(path)}
print('File counts:', folder_file_counts)
print('Folder sizes:', folder_sizes_bytes)
File counts:
{'/Users/akshay/Desktop/root/sub1': 3,
'/Users/akshay/Desktop/root/sub2': 3,
'/Users/akshay/Desktop/root/sub3': 2}
Folder sizes:
{'/Users/akshay/Desktop/root/sub1': 956211,
'/Users/akshay/Desktop/root/sub2': 643622,
'/Users/akshay/Desktop/root/sub3': 324885}
现在您可以获取最大值 counts/files 等或您想对这些词典执行的任何其他操作。
如果你有太多的文件和文件夹,这里有一个稍微更有效的代码 -
path = '/Users/akshay/Desktop/root'
folder_file_counts = []
folder_sizes_bytes = []
#Loop through the subfolders and files
filepaths = []
for r,d,f in os.walk(path):
l = []
s = []
for fname in f:
l.append(r+'/'+fname)
s.append(os.stat(r+'/'+fname).st_size)
folder_file_counts.append((r,len(l)))
folder_sizes_bytes.append((r,sum(s)))
folder_file_counts = dict(folder_file_counts)
folder_sizes_bytes = dict(folder_sizes_bytes)
print('File counts:', folder_file_counts)
print('Folder sizes:', folder_sizes_bytes)
所以目标是在目录树中找到文件数量最多的文件夹 以及使用最多磁盘的文件夹 space.
import os
path = 'C:\animal'
highestsize = ''
mostfile = ''
totalsize = 0
totalfile = 0
for i in os.listdir(path):
if not os.path.isfile(path + '\' + i):
count = 0
count_file = 0
for dirpath, subfolder, filenames in os.walk(path + '\' + i):
for file in filenames:
count_file += 1
count += os.path.getsize(os.path.join(dirpath, file))
if count_file > totalfile:
totalfile = count_file
mostfile = i
if count > totalsize:
totalsize = count
highestsize = i
print(highestsize, str(totalsize) + ' Byte')
print(mostfile + ' have the most files: ' + str(totalfile))
我设置了一个包含子文件夹和文件的虚拟文件夹,如下所示。
### Root folder with 3 sub folders, each with some files.
# root/
# sub1/
# sub2/
# sub3/
现在您可以试试这个来获取文件数和文件夹大小 -
path = '/Users/akshay/Desktop/root'
folder_file_counts = {r:len([r+'/'+files for files in f]) for r,d,f in os.walk(path)}
folder_sizes_bytes = {r:sum([os.stat(r+'/'+files).st_size for files in f]) for r,d,f in os.walk(path)}
print('File counts:', folder_file_counts)
print('Folder sizes:', folder_sizes_bytes)
File counts:
{'/Users/akshay/Desktop/root/sub1': 3,
'/Users/akshay/Desktop/root/sub2': 3,
'/Users/akshay/Desktop/root/sub3': 2}
Folder sizes:
{'/Users/akshay/Desktop/root/sub1': 956211,
'/Users/akshay/Desktop/root/sub2': 643622,
'/Users/akshay/Desktop/root/sub3': 324885}
现在您可以获取最大值 counts/files 等或您想对这些词典执行的任何其他操作。
如果你有太多的文件和文件夹,这里有一个稍微更有效的代码 -
path = '/Users/akshay/Desktop/root'
folder_file_counts = []
folder_sizes_bytes = []
#Loop through the subfolders and files
filepaths = []
for r,d,f in os.walk(path):
l = []
s = []
for fname in f:
l.append(r+'/'+fname)
s.append(os.stat(r+'/'+fname).st_size)
folder_file_counts.append((r,len(l)))
folder_sizes_bytes.append((r,sum(s)))
folder_file_counts = dict(folder_file_counts)
folder_sizes_bytes = dict(folder_sizes_bytes)
print('File counts:', folder_file_counts)
print('Folder sizes:', folder_sizes_bytes)