0016-编程打卡:按如下要求对文件夹中图片进行排序
本帖最后由 不二如是 于 2022-9-4 19:22 编辑一星答案:
import os.path
import shutil
def create_filefolder(folder_name):
if not os.path.exists(folder_name):
os.mkdir(folder_name)
def main():
create_filefolder("运行结果")
file_list = os.listdir('..\\images')
folders = []
for each in file_list:
folders.append(each.split('-'))
folders = set(folders)
os.chdir("运行结果")
for folder in folders:
create_filefolder(folder)
os.chdir('..\\..\\images')
for file in file_list:
shutil.move(file, '..\\py\\运行结果\\'+file.split('-'))
if __name__ == '__main__':
main()
二星答案:
import os
import shutil
def new_path(path):
file_dict = {}
# 转换到当前文件夹
os.chdir(path)
files = os.listdir()
# 把jpg文件找出来,建字典区分文件名和数字串
for file in files:
if file[-3:] == 'jpg':
file_dict = file.split('-')
else:
pass
# 新建文件夹,要防止文件夹已存在
for each_value in file_dict.values():
try:
os.mkdir(each_value)
except OSError:
pass
# 移动文件
for each_key in file_dict.keys():
shutil.move(each_key, file_dict)
三星答案:
**** Hidden Message *****
基础语法:
https://www.bilibili.com/video/BV1c4411e77t
算法讲解:
https://www.bilibili.com/video/BV1HT4y1K7DY
冷清加热
页:
[1]