我正在尝试根据文件大小对文件进行排序并将日志存储在文件中。但是我收到一个错误,上面写着“getsize”未定义。请帮我解决这个问题。
from ClientConfig import ClientConfig
import os
import os.path
class VerifyFileNSize:
def __init__(self):
self.config = ClientConfig()
self.parseInfo()
def parseInfo(self):
count = 0
size = 0
sort_file = []
originalPath = os.getcwd()
os.chdir(self.config.Root_Directory_Path())
log = open(self.config.File_Count(),'wb')
for root, dirs, files in os.walk("."):
for f in files:
sort_file.append(os.path.join(root, f))
sorted_file = sorted(sort_file, key=getsize)
for f in sorted_file:
log.write((str(os.path.getsize(f)) + " Bytes" + "|" + f + os.linesep).encode())
size += os.path.getsize(f)
count += 1
totalPrint = ("Client: Root:" + self.config.Root_Directory_Path() + " Total Files:" + str(count) + " Total Size in Bytes:" + str(size) + " Total Size in MB:" + str(round(size /1024/1024, 2))).encode()
print(totalPrint.decode())
log.write(totalPrint)
log.close()
os.chdir(originalPath)
if __name__ == "__main__":
VerifyFileNSize()