小编给大家分享一下使用Python脚本实现文件拷贝的方法,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
1.实现目的
统一时间对服务器某文件夹内文件进行备份保存,如若备份成功则不提示任何错误,否则将以邮件的形式告知管理员,备份出错。
2.程序流程图
主要流程图
拷贝流程图
3.代码编写
1.MyMain函数
[root@Python CheckCopyFiles]# cat MyMain.py
#!/usr/bin/env python
import os
import sys
import MyFileZip
def main() :
dir1 = "/root/dir1"
dir2 = "/root/dir2"
if os.path.isdir(dir2) == False :
os.mkdir(dir2)
if os.path.isdir(dir1) :
MyFileZip.FileCopy(dir1,dir2)
else :
sys.exit(-1)
if __name__ == "__main__" :
main()
[root@Python CheckCopyFiles]#
2.MyFile函数
[root@Python CheckCopyFiles]# cat MyFileZip.py
#!/usr/bin/env python
import zipfile
import time
import os
import MySendMail
def FileCopy(dir1,dir2) :
try:
NowDate = time.strftime("%Y-%m-%d")
Zfile = dir2+ '/'+'logfile_'+NowDate+'.zip'
zf = zipfile.ZipFile(Zfile,'w')
for filename in os.listdir(dir1) :
if os.path.isfile(dir1 + '/' + filename) :
zf.write(dir1 + '/' + filename)
os.remove(dir1 + '/' + filename)
zf.close()
except:
MySendMail.SamMail(NowDate,dir1,dir2)
[root@Python CheckCopyFiles]#
3.SendMail函数
[root@Python CheckCopyFiles]# cat MySendMail.py
#!/usr/bin/env python
import smtplib
import email.mime.text
def SamMail(NowDate,dir1,dir2) :
HOST = "smtp.163.com"
SUBJECT = "Copy File Warning"
TO = "发送的帐号"
FROM = "来自于账户"
Remask = "Copy dirctory warning"
msg = email.mime.text.MIMEText("""
<html>
<body>
<h2>Warning</h2>
<h3>Time:%s</h3>
<h3>Dirctory:%s</h3>
<h3>Remask:%s</h3>
</body>
</html>
""" % (NowDate,dir1,"Copy File Error"),"html","utf-8")
msg['Subject'] = SUBJECT
msg['From'] = FROM
msg['TO'] = TO
try:
server = smtplib.SMTP()
server.connect(HOST,'25')
server.starttls()
server.login("帐号","密码")
server.sendmail(FROM,TO,msg.as_string())
server.quit()
except:
print ("Send mail Error")
[root@Python CheckCopyFiles]#
4.实现效果
4.1 建立新的文件
[root@Python ~]# touch /root/dir1/7 /root/dir1/8
[root@Python ~]#
4.2 执行代码
[root@Python ~]# python /root/python/CheckCopyFiles/MyMain.py
[root@Python ~]#
4.3 查看文件
[root@Python ~]# ls -l /root/dir1 -a
total 0
drwxrwxrwx. 2 root root 6 Aug 27 09:26 .
dr-xr-x---. 6 root root 201 Aug 27 09:26 ..
[root@Python ~]#
[root@Python ~]# ls -l /root/dir2/
total 4
-rw-r--r--. 1 root root 218 Aug 27 09:28 logfile_2017-08-27.zip
[root@Python ~]# unzip -v /root/dir2/logfile_2017-08-27.zip
Archive: /root/dir2/logfile_2017-08-27.zip
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
0 Stored 0 0% 08-27-2017 09:27 00000000 root/dir1/7
0 Stored 0 0% 08-27-2017 09:27 00000000 root/dir1/8
-------- ------- --- -------
0 0 0% 2 files
[root@Python ~]#
由此可见,dir1中的文件已经备份至/root/dir2/logfile_2017-08-27.zip中,且已经删除了原文件
4.3 查看发送邮件效果
修改一下源码:
[root@Python CheckCopyFiles]# cat MyFileZip.py
#!/usr/bin/env python
import zipfile
import time
import os
import MySendMail
def FileCopy(dir1,dir2) :
try:
NowDate = time.strftime("%Y-%m-%d")
Zfile = dir2+ '/'+'logfile_'+NowDate+'.zip'
zf = zipfile.ZipFile(Zfile,'w')
for filename in os.listdir(dir1) :
if os.path.isfile(dir1 + '/' + filename) :
zf.write(dir1 + '/' + filename)
os.remove(dir1 + '/' + filename)
zf.close()
sys.exit()
except:
MySendMail.SamMail(NowDate,dir1,dir2)
[root@Python CheckCopyFiles]#
22行 sys 模块并未导入,故一定会出错
执行效果如下:
[root@Python ~]# python /root/python/CheckCopyFiles/MyMain.py
[root@Python ~]#
收到邮件的效果如下:
只要把脚本写入crontab,那样就可以按时执行了,例如:
[root@Python CheckCopyFiles]# crontab -l
00 14 * * 0 /usr/bin/python /root/python/CheckCopyFiles/MyMain.py
[root@Python CheckCopyFiles]#
意思是每周日14点调用/usr/bin/python 去执行/root/python/CheckCopyFiles/MyMain.py文件
看完了这篇文章,相信你对“使用Python脚本实现文件拷贝的方法”有了一定的了解,如果想了解更多相关知识,欢迎关注天达云行业资讯频道,感谢各位的阅读!