学习场景

获取壁纸软件的缓存文件到指定目录

效果

复制过程

在这里插入图片描述

源码

from shutil import copy2
from pathlib import Path
from os import listdir, path
from sys import exit


class Rename:
	def __init__(self, src=None, dst=None, suffix=None):
		"""

		:param src: 源文件夹路径
		:param dst: 目标文件夹路径
		:param suffix: 后缀名
		"""
		self.src = src
		if src is None:
			self.src = Path(Path.home()).joinpath("AppData/Roaming/360browser/bkinfo")
		self.dst = dst
		if dst is None:
			self.dst = Path(Path.home()).joinpath("Pictures/bg")
		self.suffix = "."+str(suffix)
		if suffix is None:
			self.suffix = ".jpg"
		self.fileList = []

	def exists(self):
		s = Path(self.src)
		if not s.exists():
			print("源路径不存在")
			exit(1)
		d = Path(self.dst)
		if not d.exists():
			d.mkdir()

	def get(self):
		self.fileList = listdir(self.src)
		if len(self.fileList) == 0:
			print("源文件夹找不到文件")
		for i in self.fileList:
			f = Path(self.src).joinpath(str(i))
			# print(f)
			if path.isdir(f):
				print("移除: ", i)
				self.fileList.remove(i)

	def copy(self):
		for i in self.fileList:
			src = Path(self.src).joinpath(i)
			dst = Path(self.dst).joinpath(i+self.suffix)
			print(src, "->", dst)
			try:
				copy2(src, dst)
			except Exception as e:
				print(e)

	def start(self):
		self.exists()
		self.get()
		self.copy()


# 按间距中的绿色按钮以运行脚本。
if __name__ == '__main__':
	r = Rename()
	r.start()