1.读取宋词文件,根据词人建立多个文件
import shutil
import os
if os.path.exists("wjcl/src/step3/cr"):
shutil.rmtree("wjcl/src/step3/cr")
os.mkdir("wjcl/src/step3/cr")
n = ''
f1 = open("wjcl/src/step1/宋词.txt",'r')
for i in f1:
if i.find(" ") != -1:
x = i.find(" ")
n = i[x+1:].strip()
f = open('wjcl/src/step3/cr/{}.txt'.format(n), 'a')
f.write(i)
f.close()
f1.close()
2.读取宋词文件,并根据词人建立多个文件夹
import os
import shutil
if os.path.exists("wjcl/src/step4/sccr"):
shutil.rmtree("wjcl/src/step4/sccr")
os.mkdir("wjcl/src/step4/sccr")
f1 = open("wjcl/src/step1/宋词.txt", 'r')
n = ''
for i in f1:
if i.find(" ") != -1:
x = i.find(" ")
n = i[x+1:].strip()
if os.path.exists("wjcl/src/step4/sccr/{}".format(n)):
pass
else:
os.mkdir("wjcl/src/step4/sccr/{}".format(n))
f1.close()
3.读取宋词文件,建立多个词人文件夹,为每首词在文件夹下建立文本文件
import os
import shutil
if os.path.exists("wjcl/src/step5/cr"):
shutil.rmtree("wjcl/src/step5/cr")
os.mkdir("wjcl/src/step5/cr")
f1 = open("wjcl/src/step1/宋词.txt",'r')
s1, s2 = '', ''
for i in f1:
if i.find(" ") != -1:
x = i.find(" ")
s2 = i[x+1:].strip()
s1 = i[:x].strip()
if os.path.exists("wjcl/src/step5/cr/{}".format(s2)):
pass
else:
os.mkdir("wjcl/src/step5/cr/{}".format(s2))
f = open("wjcl/src/step5/cr/{}/{}.txt".format(s2, s1), 'a')
f.write(i)
f.close()
f1.close()
4.文件的统计
import os
wjzd = {}
wjsize = {}
lj = "wjcl/src/test"
wj = os.listdir(lj)
wj.remove('.gitkeep')
for i in wj:
x, y = os.path.splitext(i)
y = y[1:]
wjzd[y] = 0
wjsize[y] = 0
for i in wj:
x, y = os.path.splitext(i)
y = y[1:]
wjzd[y] += 1
wjsize[y] += os.path.getsize("wjcl/src/test/{}".format(i))
print("文件类型txt文件数{}文件大小{:.2f}KB".format(wjzd['txt'], wjsize['txt']/1024))
print("文件类型mp3文件数{}文件大小{:.2f}KB".format(wjzd['mp3'], wjsize['mp3']/1024))
print("文件类型jpg文件数{}文件大小{:.2f}KB".format(wjzd['jpg'], wjsize['jpg']/1024))
print("文件类型pptx文件数{}文件大小{:.2f}KB".format(wjzd['pptx'], wjsize['pptx']/1024))
5.文件的移动
import os
import shutil
d = {"图片":".jpeg.jpg.png.jfif","文档":".txt.docx.pdf","音乐":".mp3.wav","影像":".mp4.flv"}
l1 = "wjcl/src/test2"
l2 = "wjcl/src/test3"
if os.path.exists(l2):
shutil.rmtree(l2)
n = os.listdir(l1)
n.remove('.gitkeep')
os.mkdir(l2)
for i in d.keys():
os.mkdir(l2+'/'+i)
for i in n:
x, y = os.path.splitext(i)
if y in d['图片']:
shutil.move(l1+'/'+i, l2+'/'+'图片')
if y in d['文档']:
shutil.move(l1+'/'+i, l2+'/'+'文档')
if y in d['影像']:
shutil.move(l1+'/'+i, l2+'/'+'影像')
if y in d['音乐']:
shutil.move(l1+'/'+i, l2+'/'+'音乐')
a = ['青城山.png', '杜甫草堂.png', '春熙路图集3.jfif',
'ifs大熊猫.jpg', '春熙路图集2.jpeg', '基地1.jpg', '春熙路图集1.jfif']
b = ['学院设置.docx', '昆明.docx', '考题四.pdf', '琵琶行并序.txt', '考题一.pdf']
c = ['pq.flv', '七彩丹霞视频.mp4', '云南十八怪_.mp4']
d = ['花儿尕恋手令.mp3', 'add.wav', 'score.wav', '山歌好比春江水.mp3']
print(a, b, c, d, sep='\n')