Concise python tutorial to backup files under windows

Under the Windows system, the zip command cannot be used. You need to download the 7z-zip or winRAR compression program to realize the zip_command to
download the latest version. After downloading: (the 7z-zip I downloaded)
1. Put 7z.exe into C:\ Under windows
2. Add the python path and C:\windows
my path to the system variable path.
path
If you run the zip_command command directly under the command line cmd, there will be an access denied error. You can run cmd.exe directly as an administrator.

Code:

#!/usr/bin/python
#Filename:backup_ver1.py

import os
import time
#1.需要备份的文件或者文件夹
source = r'E:\Python\pythonfile\test.txt'
#2.需要备份的存放目录
target_dir = r'E:\Python\pythonfile'
#3要压缩的文件
target = target_dir + time.strftime('%Y%m%d%H%M%S')+'.zip'
print (target)

zip_command = "7z a -tzip %s %s -r" %(target_dir,source)
print (zip_command)
if os.system(zip_command) == 0:
    print( 'Successfull back to',target)
else:
    print( 'backup failed')

This code is the first version in the tutorial, which notes:

1. The file path to be backed up and the destination path to save the backup need to be in the same directory. I don't know why, but if I change the others, an error
occurs. 2. The first %s in zip_command does not need to be quoted, correct The command is unquoted and the tutorial prints an error.

After a successful run it looks like this:
operation result

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326317676&siteId=291194637