猫史档案馆


【Python作品分享】极简文件分类管理【作品秀】

用户:Sx_Sx_查看:0 回复:4 评论:0 创建时间:2020-04-08T12:09:40


【作品展示】

center_image

 

【作品介绍】

文件分类管理

原创作品

 

【作品源代码】

import os
import shutil
import random
from time import *


print("欢迎使用本程序!本程序可以帮助您进行文件分类管理!")
print("注意:请自行判断输入内容会导致的后果的严重性,例如:在键入路径时输入'\\' \n因使用者个人的不当行为造成的后果于作者无关!\n")
print('''1.按时间分类
2.按大小分类\n''')
choice = str(input("请选择您的整理方式(填数字):"))

# 输入错误时的提示
while choice != '1' and choice != '2':
	choice = str(input("没有此选项!请重新输入:"))


# 方案1
if choice == '1':
	# 获取文件位置
	file_path = input("请输入您的文件所在的文件夹的绝对路径:")
	# 根据用户文件夹的名称创建新的文件夹
	folder_path = "D:/" + os.path.basename(file_path) + " time_sorted"

	while True:
		folder_path = "D:/" + os.path.basename(file_path) + " time_sorted"

		try:
			# 创建列表,用于存储路径
			file_list = []

			print("正在查找......")
			sleep(random.uniform(0.5,2))


			# 判断是否找到路径
			if os.path.exists(file_path):
				# 待用户确认

				con = input("已找到路径!是否开始(输入 是 或 否)?")
				while con != '是' and con != '否':
					con = input("输入无效!再试一次:")
				if con == '是':
					pass
				elif con == '否':
					exit()

				# 创建目录
				os.mkdir(folder_path)
				os.chdir(folder_path)
				os.makedirs("1天内")
				os.makedirs("1周内")
				os.makedirs("1月内")
				os.makedirs("1年内")
				os.makedirs("1年前")
				os.chdir(file_path)

				# 使用listdir()方法获得待整理文件夹里的文件名,并把得到的列表赋值给name变量
				name = os.listdir(file_path)

				for i in name:
					file_list.append(i)


			# 未找到路径
			else:
				print("路径不存在!请检查拼写!")
				file_path = input("请输入您的文件所在的文件夹的绝对路径:")
				continue

			# 对文件再次筛选
			today_list, a_week_list, a_month_list, a_year_list, long_ago_list = [], [], [], [], []

			for j in file_list:
				ctime = os.stat(j).st_ctime

				if time() - ctime <= 8喵00:
					today_list.append(j)
				elif time() - ctime <= 604800:
					a_week_list.append(j)
				elif time() - ctime <= 2592000:
					a_month_list.append(j)
				elif time() - ctime <= 31536000:
					a_year_list.append(j)
				else:
					long_ago_list.append(j)

			n, m, s, d, w = 0, 0, 0, 0, 0

			# 复制文件到新的文件夹
			for x in range(len(today_list)):
				if os.path.isdir(today_list[n]):
					shutil.copytree(os.path.abspath(today_list[n]), folder_path + "/1天内/" + os.path.basename(today_list\
						[n]))
				else:
					shutil.copyfile(today_list[n], folder_path + "/1天内/" + os.path.basename(today_list[n]))
				n += 1
			for x in range(len(a_week_list)):
				if os.path.isdir(a_week_list[m]):
					shutil.copytree(os.path.abspath(a_week_list[m]), folder_path + "/1周内/" + os.path.basename(a_week_list\
						[m]))
				else:
					shutil.copyfile(a_week_list[m], folder_path + "/1周内/" + os.path.basename(a_week_list[m]))
				m += 1
			for x in range(len(a_month_list)):
				if os.path.isdir(a_month_list[s]):
					shutil.copytree(os.path.abspath(a_month_list[s]), folder_path + "/1月内/" + os.path.basename(a_month_list\
						[s]))
				else:
					shutil.copyfile(a_month_list[s], folder_path + "/1月内/" + os.path.basename(a_month_list[s]))
				s += 1
			for x in range(len(a_year_list)):
				if os.path.isdir(a_year_list[d]):
					shutil.copytree(os.path.abspath(a_year_list[d]), folder_path + "/1年内/" + os.path.basename(a_year_list\
						[d]))
				else:
					shutil.copyfile(a_year_list[d], folder_path + "/1年内/" + os.path.basename(a_year_list[d]))
				d += 1
			for x in range(len(long_ago_list)):
				if os.path.isdir(long_ago_list[w]):
					shutil.copytree(os.path.abspath(long_ago_list[w]), folder_path + "/1年前/" + os.path.basename(long_ago_list\
						[w]))
				else:
					shutil.copyfile(long_ago_list[w], folder_path + "/1年前/" + os.path.basename(long_ago_list[w]))
				w += 1
			break

		# 如果文件已整理(文件夹重名)
		except FileExistsError:
			res = input("您已经整理过该文件了哦!是否重新整理?(输入 是 或 否)")
			while res != '是' and res != '否':
				res = input("输入无效!再试一次:")
			if res == '是':
				# 删除原文件夹后重新整理
				shutil.rmtree(folder_path)
				continue
			else:
				# 直接退出程序
				exit()

	print(r"文件已整理完毕!位置:" + folder_path)
	sleep(3)
	os.system("pause")


# 方案2
elif choice == '2':
	# 获取文件位置
	file_path = input("请输入您的文件所在的文件夹的绝对路径:")
	# 根据用户文件夹的名称创建新的文件夹
	folder_path = "D:/" + os.path.basename(file_path) + " size_sorted"

	while True:
		folder_path = "D:/" + os.path.basename(file_path) + " size_sorted"
		try:
			# 创建列表,用于存储路径
			file_list = []
			print("正在查找......")
			sleep(random.uniform(0.5,2))


			# 判断是否找到路径
			if os.path.exists(file_path):
				# 待用户确认

				con = input("已找到路径!是否开始(输入 是 或 否)?")
				while con != '是' and con != '否':
					con = input("输入无效!再试一次:")
				if con == '是':
					pass
				elif con == '否':
					exit()

				# 创建目录
				os.mkdir(folder_path)
				os.chdir(folder_path)
				os.makedirs("1KB内")
				os.makedirs("10MB内")
				os.makedirs("512MB内")
				os.makedirs("2GB内")
				os.makedirs("2GB以上")
				os.chdir(file_path)

				# 使用listdir()方法获得待整理文件夹里的文件名,并把得到的列表赋值给name变量
				name = os.listdir(file_path)

				for i in name:
					file_list.append(i)


			# 未找到路径
			else:
				print("路径不存在!请检查拼写!")
				file_path = input("请输入您的文件所在的文件夹的绝对路径:")
				continue

			# 对文件再次筛选
			a_KB_list, aKB_to_10MB_list, m_512MB_list, m_2GB_list, big_list = [], [], [], [], []
			for j in file_list:
				msize = os.stat(j).st_size
				if os.path.isdir(j):
					msize = 0
					for root, dirs, files in os.walk(os.path.abspath(j)):
						msize += sum([os.path.getsize(os.path.join(root, name)) for name in files])
				if msize < 1024:
					a_KB_list.append(j)
				elif msize <= 10485760:
					aKB_to_10MB_list.append(j)
				elif msize <= 536870912:
					m_512MB_list.append(j)
				elif msize <= 2147483喵8:
					m_2GB_list.append(j)
				else:
					big_list.append(j)

			n, m, s, d, w = 0, 0, 0, 0, 0

			# 复制文件到新的文件夹
			for x in range(len(a_KB_list)):
				if os.path.isdir(a_KB_list[n]):
					shutil.copytree(os.path.abspath(a_KB_list[n]), folder_path + "/1KB内/" + os.path.basename(a_KB_list\
						[n]))
				else:
					shutil.copyfile(a_KB_list[n], folder_path + "/1KB内/" + os.path.basename(a_KB_list[n]))
				n += 1
			for x in range(len(aKB_to_10MB_list)):
				if os.path.isdir(aKB_to_10MB_list[m]):
					shutil.copytree(os.path.abspath(aKB_to_10MB_list[m]), folder_path + "/10MB内/" + os.path.basename(aKB_to_10MB_list\
						[m]))
				else:
					shutil.copyfile(aKB_to_10MB_list[m], folder_path + "/10MB内/" + os.path.basename(aKB_to_10MB_list[m]))
				m += 1
			for x in range(len(m_512MB_list)):
				if os.path.isdir(m_512MB_list[s]):
					shutil.copytree(os.path.abspath(m_512MB_list[s]), folder_path + "/512MB内/" + os.path.basename(m_512MB_list\
						[s]))
				else:
					shutil.copyfile(m_512MB_list[s], folder_path + "/512MB内/" + os.path.basename(m_512MB_list[s]))
				s += 1
			for x in range(len(m_2GB_list)):
				if os.path.isdir(m_2GB_list[d]):
					shutil.copytree(os.path.abspath(m_2GB_list[d]), folder_path + "/2GB内/" + os.path.basename(m_2GB_list\
						[d]))
				else:
					shutil.copyfile(m_2GB_list[d], folder_path + "/2GB内/" + os.path.basename(m_2GB_list[d]))
				d += 1
			for x in range(len(big_list)):
				if os.path.isdir(big_list[w]):
					shutil.copytree(os.path.abspath(big_list[w]), folder_path + "/2GB以上/" + os.path.basename(big_list\
						[w]))
				else:
					shutil.copyfile(big_list[w], folder_path + "/2GB以上/" + os.path.basename(big_list[w]))
				w += 1
			break

		# 如果文件已整理(文件夹重名)
		except FileExistsError:
			res = input("您已经整理过该文件了哦!是否重新整理?(输入 是 或 否)")
			while res != '是' and res != '否':
				res = input("输入无效!再试一次:")
			if res == '是':
				# 删除原文件夹后重新整理
				shutil.rmtree(folder_path)
				continue
			else:
				# 直接退出程序
				exit()

	print(r"文件已整理完毕!位置:" + folder_path)
	sleep(3)
	os.system("pause")

exit()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
我逝杉风我逝杉风

膜拜大佬

 

点赞1


评论


沉著的飞叶龙uZWJ沉著的飞叶龙uZWJ

os大佬喵

点赞1


评论


凉飘飘fly凉飘飘fly

一脸懵13地看完了这篇帖子(然鹅一个词儿都没看懂)

点赞0


评论


Sx_Sx_

🌚

点赞0


评论