#encoding=utf-8
"""
遍历指定目录下的所有文件,找出其中占用空间最大的前3个文件
"""
import os
def searchMaxFiles(path):
os.chdir(path)
docsAndFiles=os.listdir(path)
fileSizes=[]
fileDicts={}
for file in docsAndFiles:
if os.path.isfile(file):
fileSizes.append(os.path.getsize(file))
fileDicts[os.path.getsize(file)]=file
for i in range(len(fileSizes)-1):
for j in range(len(fileSizes)-i-1):
if fileSizes[j]>fileSizes[j+1]:
fileSizes[j],fileSizes[j+1] =fileSizes[j+1],fileSizes[j]
print("文件大小列表:%s\n文件字典:%s"%(fileSizes,fileDicts))
for size in fileSizes[len(fileSizes):len(fileSizes)-4:-1]:
if size in fileDicts.keys():
print("文件的的大小:%s文件名称:%s"%(size,fileDicts[size]))
print(searchMaxFiles("e:\\murphy\\file"))
#encoding=utf-8
"""
过滤py源码中的#注释,另存为文件result.py,并执行result.py,断言是否执行成功
"""
import os
def filterAndOperation(path):
fp=open(path,"r",encoding="utf8")
for line in fp:
#print(line)
if not line.startswith("#"):
if "#" in line:
temp=[]
temp=list(line)
wLine=""
order=line.index("#")
wLine="".join(temp[:order])
#print(wLine)
#print(wLine)
with open("e:\\murphy\\file\\result.py","a",encoding="utf8") as fpro:
fpro.write(wLine+"\n")
else:
with open("e:\\murphy\\file\\result.py","a") as fpro:
fpro.write(line)
fp.close()
os.chdir("e:\\murphy\\file")
os.system("python result.py > result.txt")
with open("e:\\murphy\\file\\result.txt","r",encoding="utf8") as fpro1:
print(fpro1.readlines())
print(filterAndOperation("e:\\murphy\\file\\area.py"))