python implements file backup script under windows
- 2020-10-23 20:09:04
- OfStack
Backup files under windows using python scripts, refer to A Byte of Python3 chapter 101 (Page59).
#!/usr/bin/python
# -*- coding: cp936 -*-
import os
import time
source = ['E:\\'] # The backup file is in E Under the root directory
running = True
while running:
your_source = raw_input("Your own path or your own file path:")
# If you are using input() When entering the pathname after run, you need to add it to both sides " " , the following input In the same way
# Like you want to back up E Under the plate zipme In the folder hello.txt File, should be entered zipme\\hello.txt
source.append(your_source)
if raw_input("Do you want to add file or folder(y/n):")=='n':
running = False
target_dir = 'E:\\backup\\' # Backup the path where the generated files are stored
# A compressed file that is generated with the current date and time as the file name
target = target_dir+\
time.strftime('%Y')+\
time.strftime('%m')+\
time.strftime('%d')+\
time.strftime('%H')+\
time.strftime('%M')+\
time.strftime('%S')+'.rar'
# use zip Command compression file
zip_command = "zip -qr {0} {1}".format(target, ''.join(source))
# The compression command is executed by passing parameters to the system WinRAR With the file rar.exe To perform compression)
if os.system(zip_command) == 0:
print('Successful backup to',target)
else:
print('Backup FAILED')