import os
df=os.listdir("wjcl/src/step3/cr")
for x in df:
if 'txt' in x:
os.remove("wjcl/src/step3/cr/"+x)
f1=open("wjcl/src/step1/宋词.txt",'r')
#代码开始
for line in f1:#可以使用迭代
if " " in line.strip("\n"):#如果包含空格:
pos=line.find(" ")#pos为空格所在位置
xm=line.strip("\n")[pos+1:]#截取姓名
f2=open("wjcl/src/step3/cr/"+xm+".txt",'a+')#以追加的方式打开文件xm.txt(不存在就新建,存在就打开)
f2.write(line)#将读出的内容写入文件
#代码结束
f1.close()
f2.close()
第3关 读取宋词文件,建立多个词人文件夹,为每首词在文件夹下建立文本文件
import os
for root, dirs, files in os.walk("wjcl/src/step5/cr", topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
if not os.path.exists("wjcl/src/step5/cr"):
os.mkdir("wjcl/src/step5/cr")
f1=open("wjcl/src/step1/宋词.txt",'r')
#代码开始
sc=""
for line in f1:#可以使用迭代
if " " in line.strip("\n"):#如果包含空格
if len(sc)>0:
f2.write(sc)#把上一首诗的内容写入文件
f2.close()
pos=line.find(" ")#pos为空格所在位置
cm=line[:pos].strip('\n')#取出诗名
xm=line.strip("\n")[pos+1:]#截取姓名
lj="wjcl/src/step5/cr/"+xm#生成文件夹名称scsr\诗人
if not os.path.exists(lj):#若存在此文件夹
os.mkdir(lj)#建立诗人文件夹
f2=open(lj+"/"+cm+".txt","w")
sc=""#sc保存诗词内容 此时为空
sc=sc+line
if len(sc)>0:
f2.write(sc)
f2.close()
f1.close()
#代码结束
第4关 文件的统计
import os
wjzd={}
wjsize={}
lj=lj="wjcl/src/test"
#代码开始
import os
from os.path import join, getsize
dir="wjcl/src/test"
number=0
number1=0
number2=0
number3=0
size=0
size1=0
size2=0
size3=0
w1=0
w2=0
w3=0
for root,dirs,files in os.walk(dir):
for file in files:
if os.path.splitext(file)[1] == '.txt':
q = os.path.splitext(file)[1]
w = q.strip('.')
number += 1
elif os.path.splitext(file)[1] == '.mp3':
q1 = os.path.splitext(file)[1]
w1 = q1.strip('.')
number1 += 1
elif os.path.splitext(file)[1] == '.jpg':
q2 = os.path.splitext(file)[1]
w2 = q2.strip('.')
number2 += 1
elif os.path.splitext(file)[1] == '.pptx':
q3 = os.path.splitext(file)[1]
w3 = q3.strip('.')
number3 += 1
size+=sum([getsize(join(root,name))for name in files if os.path.splitext(name)[1] == '.txt'])
size1+=sum([getsize(join(root,name))for name in files if os.path.splitext(name)[1] == '.mp3'])
size2+=sum([getsize(join(root,name))for name in files if os.path.splitext(name)[1] == '.jpg'])
size3+=sum([getsize(join(root,name))for name in files if os.path.splitext(name)[1] == '.pptx'])
print("文件类型{}文件数{}文件大小{:.2f}KB".format(w,number,size/1024))
print("文件类型{}文件数{}文件大小{:.2f}KB".format(w1,number1,size1/1024))
print("文件类型{}文件数{}文件大小{:.2f}KB".format(w2,number2,size2/1024))
print("文件类型{}文件数{}文件大小{:.2f}KB".format(w3,number3,size3/1024))
#代码结束
for x in wjzd:
print("文件类型{}文件数{}文件大小{:.2f}KB".format(x,wjzd[x],wjsize[x]/1024))
第5关 文件的移动
import os,shutil
dir="wjcl/src/test2"
m1="wjcl/src/test3"
a1="wjcl/src/test2/图片"
a2="wjcl/src/test2/文档"
a3="wjcl/src/test2/音乐"
a4="wjcl/src/test2/影像"
os.mkdir(m1)
os.mkdir(a1)
os.mkdir(a2)
os.mkdir(a3)
os.mkdir(a4)
for file in os.listdir(dir):
if os.path.splitext(file)[1] == '.jpeg' or os.path.splitext(file)[1] == '.jpg' or os.path.splitext(file)[1] == '.png' or os.path.splitext(file)[1] == '.jfif':
s = os.path.join(dir,file)
d = os.path.join(a1,file)
shutil.move(s,d)
elif os.path.splitext(file)[1] == '.txt' or os.path.splitext(file)[1] == '.docx' or os.path.splitext(file)[1] == '.pdf':
s1 = os.path.join(dir,file)
d1 = os.path.join(a2,file)
shutil.move(s1,d1)
elif os.path.splitext(file)[1] == '.mp3' or os.path.splitext(file)[1] == '.wav':
s2 = os.path.join(dir,file)
d2 = os.path.join(a3,file)
shutil.move(s2,d2)
elif os.path.splitext(file)[1] == '.mp4' or os.path.splitext(file)[1] == '.flv':
s3 = os.path.join(dir,file)
d3 = os.path.join(a4,file)
shutil.move(s3,d3)
print(os.listdir(a1))
print(os.listdir(a2))
print(os.listdir(a4))
print(os.listdir(a3))