【Python】-小工具-【Windows】拷贝目录下所有文件到另一个目录

#拷贝当前目录下的文件夹所有内容
#1、lexists和exists的区别还需要继续学习
#2、Readline后面会带着换行符,直接赋值给exist()函数总会返回false,需要strip掉'\n'换行符
import os.path
from shutil import copytree,ignore_patterns
'''
功能:通过task.txt文档中的文件夹名字,拷贝对应目录文件--》指定盘符下面
1、copyTree
2、os.path.exist()
3、strip去掉readlines的换行符
'''

winDriver = "E:"
newDictory = "西瓜视频"

try:
	
	#print(os.environ)
	currentDir = os.path.abspath(os.curdir)

	count = 0
	
	with open(file = "task.txt",mode = 'r',encoding = "utf-8") as fp:
		ls = fp.readlines()
	for item in ls:
		count+=1
		fsrc = os.path.join(currentDir,item.strip('\n'))
		fdst = os.path.join(winDriver,os.sep,newDictory,item.strip('\n'))
		print(count,end = '')
		#print(fsrc)
		#print(os.path.lexists(fsrc))

		if os.path.lexists(fsrc):
			if not os.path.lexists(fdst):
				copytree(fsrc ,fdst,ignore=ignore_patterns('*.wav', '*.jpg'))
				print("拷贝完成",fdst)
			else:
				print("已存在",fdst)
		else:
			print("不存在",fsrc)		
		fsrc = ""
		fdst = ""

except IOError as e:
	pass
	
finally:
	pass
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章