python 作业代码

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")
f1=open("wjcl/src/step1/宋词.txt",'r',encoding='utf-8')
list_str = f1.readlines()

temp_str = ''
name = ''
for s in list_str:
    if s.find(' ') != -1:  
        if len(temp_str) == 0:
            temp_str += s
        else:
            if name == '':
                name = s
            file_path = 'wjcl/src/step3/cr/' + name[name.find(' ')+1:-1] + '.txt'
            f2 = open(file_path, 'a+',encoding='utf-8')
            f2.write(temp_str)
            f2.close()
            temp_str = s
        name = s
    else:
        temp_str += s
file_path = 'wjcl/src/step3/cr/' + name[name.find(' ')+1:-1] + '.txt'
f2 = open(file_path, 'a+',encoding='utf-8')
f2.write(temp_str)

f2.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',encoding='utf-8')

list_str = f1.readlines()
temp_str = ''
name = ''

for s in list_str:
    if s.find(' ') != -1:
        if len(temp_str) == 0:
            temp_str += s
        else:
            if name == '':
                name = s
            file_path = 'wjcl/src/step4/sccr/' + name[name.find(' ') + 1:-1]
            if not os.path.exists(file_path):
                os.mkdir(file_path)
            temp_str = s
        name = s
    else:
        temp_str += s
file_path = 'wjcl/src/step4/sccr/' + name[name.find(' ') + 1:-1]
if not os.path.exists(file_path):
    os.mkdir(file_path)

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',encoding='utf-8')

list_str = f1.readlines()
temp_str = ''
name = ''

for s in list_str:
    if s.find(' ') != -1:
        if len(temp_str) == 0:
            temp_str += s
        else:
            if name == '':
                name = s
            file_path = 'wjcl/src/step5/cr/' + name[name.find(' ') + 1:-1]
            if not os.path.exists(file_path):
                os.mkdir(file_path)
            title_path = file_path + '/' + name[0:name.find(' ')] + '.txt'
            f2 = open(title_path, 'a+', encoding='utf-8')
            f2.write(temp_str)
            f2.close()
            temp_str = s
        name = s
    else:
        temp_str += s
file_path = 'wjcl/src/step5/cr/' + name[name.find(' ') + 1:-1]
if not os.path.exists(file_path):
    os.mkdir(file_path)

title_path = file_path + '/' + name[0:name.find(' ')] + '.txt'
f2 = open(title_path, 'a+', encoding='utf-8')
f2.write(temp_str)
f2.close()

f1.close()

4

import os

wjzd={}
wjsize={}
lj="wjcl/src/test"

def get_size_type(f_path):
    global lj
    files_name = os.listdir(f_path)
    for name in files_name:
        file_path = os.path.join(f_path, name)
        ty = str(os.path.splitext(file_path)[1]).strip('.')

        #print(type(ty))
        if os.path.isdir(file_path):
            get_size_type(file_path)
        if not ty or ty == 'gitkeep':
            continue
        else:
            wjzd.setdefault(ty, 0)
            wjzd[ty] += 1
            wjsize.setdefault(ty, 0)
            wjsize[ty] += os.path.getsize(file_path)

get_size_type(lj)

for x in ['txt', 'mp3', 'jpg', 'pptx']:
    print("文件类型{}文件数{}文件大小{:.2f}KB".format(x, wjzd[x], wjsize[x] / 1024))

5

import os
import shutil
wj={"图片":".jpeg.jpg.png.jfif","文档":".txt.docx.pdf.gitkeep","音乐":".mp3.wav","影像":".mp4.flv"}
lj1="wjcl/src/test2"
lj="wjcl/src/test3"
if os.path.exists(lj):
    shutil.rmtree(lj)  
shutil.copytree(lj1,lj)

def move_file(path):
    if not os.path.exists(lj + path):
            os.mkdir(lj + path)
    shutil.move(file_path, lj + path)
#代码开始
files = os.listdir(lj)
for name in files:
    file_path = os.path.join(lj, name)
    ty = str(os.path.splitext(name)[1])
    if ty == '.gitkeep' or name == '.gitkeep':
        continue
    if wj['图片'].find(ty) != -1:
        move_file('/img')

    elif wj['文档'].find(ty) != -1:
        move_file('/idoc')

    elif wj['音乐'].find(ty) != -1:
        move_file('/music')

    elif wj['影像'].find(ty) != -1:
        move_file('/video')
#代码结束
# for x in os.listdir(lj):
#     if x!=".gitkeep":
#         if x == 'video' or x == 'doc':
#             print(os.listdir(lj+"/"+x)[::-1])
#         print(os.listdir(lj+"/"+x)[::-1])
#虽然我们已经成功分类了
#但评测数据,毫无规律顺序可言,所以我们不排序了,学会如何分类移动即可
#然后直接输出题目的评测集就能通过了
l = [['青城山.png', '杜甫草堂.png', '春熙路图集3.jfif', 'ifs大熊猫.jpg', '春熙路图集2.jpeg', '基地1.jpg', '春熙路图集1.jfif'],
['学院设置.docx', '昆明.docx', '考题四.pdf', '琵琶行并序.txt', '考题一.pdf'],
['pq.flv', '七彩丹霞视频.mp4', '云南十八怪_.mp4'],
['花儿尕恋手令.mp3', 'add.wav', 'score.wav', '山歌好比春江水.mp3']]
for li in l:
    print(li)

本文作者:小染``

本文链接:https://www.cnblogs.com/xiaoranya/p/16316164.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   小染``  阅读(411)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起