如何使用Python腳本實(shí)現(xiàn)文件拷貝
這篇文章主要介紹了如何使用Python腳本實(shí)現(xiàn)文件拷貝,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1.實(shí)現(xiàn)目的
統(tǒng)一時(shí)間對(duì)服務(wù)器某文件夾內(nèi)文件進(jìn)行備份保存,如若備份成功則不提示任何錯(cuò)誤,否則將以郵件的形式告知管理員,備份出錯(cuò)。
2.程序流程圖
主要流程圖

拷貝流程圖

3.代碼編寫
1.MyMain函數(shù)
[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函數(shù)
[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函數(shù)
[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 = "發(fā)送的帳號(hào)"
FROM = "來(lái)自于賬戶"
Remask = "Copy dirctory warning"
msg = email.mime.text.MIMEText("""
<html>
<body>
<h1>Warning</h1>
<h2>Time:%s</h2>
<h2>Dirctory:%s</h2>
<h2>Remask:%s</h2>
</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("帳號(hào)","密碼")
server.sendmail(FROM,TO,msg.as_string())
server.quit()
except:
print ("Send mail Error")
[root@Python CheckCopyFiles]#
4.實(shí)現(xiàn)效果
4.1 建立新的文件
[root@Python ~]# touch /root/dir1/7 /root/dir1/8 [root@Python ~]#
4.2 執(zhí)行代碼
[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中的文件已經(jīng)備份至/root/dir2/logfile_2017-08-27.zip中,且已經(jīng)刪除了原文件
4.3 查看發(fā)送郵件效果
修改一下源碼:
[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 模塊并未導(dǎo)入,故一定會(huì)出錯(cuò)
執(zhí)行效果如下:
[root@Python ~]# python /root/python/CheckCopyFiles/MyMain.py [root@Python ~]#
收到郵件的效果如下:

只要把腳本寫入crontab,那樣就可以按時(shí)執(zhí)行了,例如:
[root@Python CheckCopyFiles]# crontab -l 00 14 * * 0 /usr/bin/python /root/python/CheckCopyFiles/MyMain.py [root@Python CheckCopyFiles]#
意思是每周日14點(diǎn)調(diào)用/usr/bin/python 去執(zhí)行/root/python/CheckCopyFiles/MyMain.py文件
至此,由python寫的備份腳本已經(jīng)全部完成
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
pytorch下大型數(shù)據(jù)集(大型圖片)的導(dǎo)入方式
今天小編就為大家分享一篇pytorch下大型數(shù)據(jù)集(大型圖片)的導(dǎo)入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
Flask框架實(shí)現(xiàn)debug模式下計(jì)算pin碼
pin碼也就是flask在開啟debug模式下,進(jìn)行代碼調(diào)試模式的進(jìn)入密碼。本文為大家整理了Flask框架在debug模式下計(jì)算pin碼的方法,需要的可以參考一下2023-02-02
用Python實(shí)現(xiàn)協(xié)同過(guò)濾的教程
這篇文章主要介紹了用Python實(shí)現(xiàn)協(xié)同過(guò)濾的教程,主要用于從大數(shù)據(jù)中抽取用戶信息偏好等等,需要的朋友可以參考下2015-04-04
python中print函數(shù)的用法示例與詳細(xì)講解
這篇文章主要給大家介紹了關(guān)于python中print函數(shù)的用法示例與詳細(xì)講解,print()函數(shù)可以將輸出的信息打印出來(lái),即發(fā)送給標(biāo)準(zhǔn)輸出流,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05
django使用haystack調(diào)用Elasticsearch實(shí)現(xiàn)索引搜索
這篇文章主要介紹了django使用haystack調(diào)用Elasticsearch實(shí)現(xiàn)索引搜索,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
解決python3插入mysql時(shí)內(nèi)容帶有引號(hào)的問(wèn)題
今天小編就為大家分享一篇解決python3插入mysql時(shí)內(nèi)容帶有引號(hào)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
python基于tkinter實(shí)現(xiàn)gif錄屏功能
一直在思索實(shí)現(xiàn)一個(gè)透明的窗體,然后可以基于這個(gè)窗體可以開發(fā)出各種好玩的應(yīng)用,這一期,我們將實(shí)現(xiàn)有趣的GIF錄屏功能2021-05-05
jupyter 使用Pillow包顯示圖像時(shí)inline顯示方式
這篇文章主要介紹了jupyter 使用Pillow包顯示圖像時(shí)inline顯示方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04

