用Python 实现的目录拷贝程序
- 作者: 日艹芊芊三百回
- 来源: 51数据库
- 2022-08-12
用python写的小程序,可以用这个程序在固定的目录之间来回的拷贝.
copy one directory to another directory
@author:
'''
import os;
import shutil, errno;
import ctypes;
import itertools;
import string;
import platform;
_home = "E:\sourcecode";
home_disk = "K:\FTS_HOME";
office = "D:\sourcecode\FTS";
office_disk = "E:\FTS_HOME";
other_source = "D:\\pythontest\\source";
other_target = "D:\\pythontest\\target";
FILES_DIRECTORY_LIST = ["ForeignTradeFront\src", "ForeignTradeFront\WebContent", "ForeignTradeServer\ejbModule", "ForeignTradeServer\TestSrc"];
def copyFiles():
directory = input("how to copy files? \n\
copy to office(1), \n\
copy to office removable driver(2),\n\
copy to _home(3),\n\
copy to _home removable driver(4);\n\
other(5)");
if directory == "1":
executeCopyFiles(office_disk, office);
elif directory == "2":
executeCopyFiles(office, office_disk);
elif directory == "3":
executeCopyFiles(home_disk, _home);
elif directory == "4":
executeCopyFiles(_home, home_disk);
elif directory == "5":
executeCopyFiles(other_source, other_target);
def executeCopyFiles(sourceDirectory, targetDirectory):
copyConfirm = input("Are you sure want copy" + sourceDirectory + " to " + targetDirectory + "?(Y/N)");
if copyConfirm == "Y":
if os.path.isdir(sourceDirectory) and os.path.isdir(targetDirectory):
for update_directory in FILES_DIRECTORY_LIST:
newSourceDirectory = generateUpdatePath(sourceDirectory, update_directory) ;
newTargetDirectory = generateUpdatePath(targetDirectory, update_directory) ;
deleteOldTargetDirectory(newTargetDirectory);
copyanything(newSourceDirectory, newTargetDirectory);
else:
print("Please input a directionary path!");
else:
print("Cancellation copy file!");
def generateUpdatePath(originalPath, updatePath):
return os.path.join(originalPath, updatePath);
def deleteOldTargetDirectory(targetDirectory):
print(targetDirectory + " was removed!");
shutil.rmtree(targetDirectory, True);
def copyanything(src, dst):
try:
if(os.path.exists(dst)):
shutil.rmtree(dst, False);
shutil.copytree(src, dst)
print(dst + " copy successful");
except OSError as exc: # python >2.5
if exc.errno == errno.ENOTDIR:
shutil.copy(src, dst)
else: raise
if __name__ == '__main__':
copyFiles();
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
