国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現(xiàn)備份目錄的方法

2019-11-25 17:06:22
字體:
來源:轉載
供稿:網友

本文實例講述了python實現(xiàn)備份目錄的方法。分享給大家供大家參考。具體如下:

備份腳本1:

#!/usr/bin/python# Filename: backup_ver1.pyimport osimport time# 1. The files and directories to be backed up are specified in a list.source = ['/home/swaroop/byte', '/home/swaroop/bin']# If you are using Windows, use source = [r'C:/Documents', r'D:/Work'] or something like that# 2. The backup must be stored in a main backup directorytarget_dir = '/mnt/e/backup/' # Remember to change this to what you will be using# 3. The files are backed up into a zip file.# 4. The name of the zip archive is the current date and timetarget = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'# 5. We use the zip command (in Unix/Linux) to put the files in a zip archivezip_command = "zip -qr '%s' %s" % (target, ' '.join(source))# Run the backupif os.system(zip_command) == 0:  print 'Successful backup to', targetelse:  print 'Backup FAILED'

輸出:

$ python backup_ver1.py
Successful backup to /mnt/e/backup/20041208073244.zip

備份腳本2:

#!/usr/bin/python# Filename: backup_ver2.pyimport osimport time# 1. The files and directories to be backed up are specified in a list.source = ['/home/swaroop/byte', '/home/swaroop/bin']# If you are using Windows, use source = [r'C:/Documents', r'D:/Work'] or something like that# 2. The backup must be stored in a main backup directorytarget_dir = '/mnt/e/backup/' # Remember to change this to what you will be using# 3. The files are backed up into a zip file.# 4. The current day is the name of the subdirectory in the main directorytoday = target_dir + time.strftime('%Y%m%d')# The current time is the name of the zip archivenow = time.strftime('%H%M%S')# Create the subdirectory if it isn't already thereif not os.path.exists(today):  os.mkdir(today) # make directory  print 'Successfully created directory', today# The name of the zip filetarget = today + os.sep + now + '.zip'# 5. We use the zip command (in Unix/Linux) to put the files in a zip archivezip_command = "zip -qr '%s' %s" % (target, ' '.join(source))# Run the backupif os.system(zip_command) == 0:  print 'Successful backup to', targetelse:  print 'Backup FAILED'

輸出:

$ python backup_ver2.py
Successfully created directory /mnt/e/backup/20041208
Successful backup to /mnt/e/backup/20041208/080020.zip
$ python backup_ver2.py
Successful backup to /mnt/e/backup/20041208/080428.zip

備份腳本3:

#!/usr/bin/python# Filename: backup_ver4.pyimport osimport time# 1. The files and directories to be backed up are specified in a list.source = ['/home/swaroop/byte', '/home/swaroop/bin']# If you are using Windows, use source = [r'C:/Documents', r'D:/Work'] or something like that# 2. The backup must be stored in a main backup directorytarget_dir = '/mnt/e/backup/' # Remember to change this to what you will be using# 3. The files are backed up into a zip file.# 4. The current day is the name of the subdirectory in the main directorytoday = target_dir + time.strftime('%Y%m%d')# The current time is the name of the zip archivenow = time.strftime('%H%M%S')# Take a comment from the user to create the name of the zip filecomment = raw_input('Enter a comment --> ')if len(comment) == 0: # check if a comment was entered  target = today + os.sep + now + '.zip'else:  target = today + os.sep + now + '_' + /    comment.replace(' ', '_') + '.zip'  # Notice the backslash!# Create the subdirectory if it isn't already thereif not os.path.exists(today):  os.mkdir(today) # make directory  print 'Successfully created directory', today# 5. We use the zip command (in Unix/Linux) to put the files in a zip archivezip_command = "zip -qr '%s' %s" % (target, ' '.join(source))# Run the backupif os.system(zip_command) == 0:  print 'Successful backup to', targetelse:  print 'Backup FAILED'

輸出:

$ python backup_ver4.py
Enter a comment --> added new examples
Successful backup to /mnt/e/backup/20041208/082156_added_new_examples.zip
$ python backup_ver4.py
Enter a comment -->
Successful backup to /mnt/e/backup/20041208/082316.zip

希望本文所述對大家的Python程序設計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 仁化县| 颍上县| 襄垣县| 韩城市| 平原县| 新泰市| 林西县| 盐山县| 武隆县| 故城县| 吴江市| 于田县| 扎鲁特旗| 玉溪市| 湘潭市| 清流县| 四会市| 宿迁市| 松潘县| 双江| 宣恩县| 喀喇| 醴陵市| 滨州市| 榕江县| 德令哈市| 石台县| 西平县| 闽清县| 左云县| 乌鲁木齐县| 玛多县| 新兴县| 姜堰市| 陇西县| 临汾市| 鄢陵县| 恩施市| 余江县| 榆树市| 余江县|