Python simple Backup File Script v1.0 instance,

Source: Internet
Author: User
Tags month name

Python simple Backup File Script v1.0 instance,

Overall Thinking

The directory to be backed up is listed as a list, which is compressed and backed up by executing system commands.

The key lies in constructing commands and using OS. system () to execute. At first, the zip command was never successful. Later, we found that this command was not available in Windows. We also needed to install the GnuWin32 project, and then installed 7z, implements compression using system commands.

Compression command

After the 7zcompressed file is downloaded, add 7z.exe 7z and dll to the directory of system environment variables. Run the following command to compress and decompress 7z a test.zip a.txt B .txt # to specify a number of files.

7z a test.zip f:/test/** # The compressed folder 7z x test.zip-of: \ test #-o indicates the output directory. Note that there is no space between it and the directory path.
7z x test.zip-o "f: \ test abc" # enclose the Output Folder in quotation marks if there are spaces.

Test:

C:\Users\hm\Desktop\Web>7z a tes.7z python362.chm7-Zip 17.01 beta (x64) : Copyright (c) 1999-2017 Igor Pavlov : 2017-08-28Scanning the drive:file, 8010498 bytes (7823 KiB)Creating archive: tes.7zAdd new data to archive: 1 file, 8010498 bytes (7823 KiB)Files read from disk: 1Archive size: 7982833 bytes (7796 KiB)Everything is Ok

Compile Python backup code

After the 7z command is compressed, write the Python backup code:

# Filename: backup. pyimport OS, time # list of files to be backed up source = ['C: \ Users \ hm \ Desktop \ Web \ ch2', 'c: \ Users \ hm \ Desktop \ Web \ CH3 '] # construct the target backup file target_dir = 'd: \ backup 'target = target_dir + OS. sep + time.strftime('policy?m=d=h?m=s'{'.7z' # strftime specifies the time format. For more information about this function # construct the system command line command = "7z a {0} {1 }". format (target ,''. join (source) # Do I Need To use double quotation marks if a string contains spaces? # Print (command) # If the execution is successful, the 0if OS is returned. system (command) = 0: print ('backup successful to ', target) else: print ('backup failed ')

This error was first encountered because the PATH uses the 'C: \ user' representation.

SyntaxError
---------------------------
(Unicode error) 'unicodeshell' codec can't decode bytes in position 2-3: truncated \ UXXXXXXXX escape

The cause of this error is: "\" in the Code represents escape in Python.

1. Add r or R before the string, that is:

source = [r'C:\Users\hm\Desktop\Web\CH2',r'C:\Users\hm\Desktop\Web\CH3']

Here, r or R represents a non-escape string in python.

2. Add "\" before "\" to implement escape.

3. Replace "\" with "/".

The backup is successful after the error is corrected.

Backup successful to D: \ backup \ 201711051520.8.7z

About strftimr Functions

The function information is as follows:

Syntax

Strftime () method Syntax:

Time. strftime (format [, t]) parameter
• Format -- format string.
• T -- the optional parameter t is a struct_time object.
Time and date formatting symbols in python:
• % Y two-digit year representation (00-99)
• % Y indicates the four-digit year (000-9999)
• % M month (01-12)
• One Day in % d (0-31)
• % H 24-hour (0-23)
• % I 12-hour (01-12)
• % M minutes (00 = 59)
• % S seconds (00-59)
• % A local simplified week name
• % A local full week name
• % B simplified local month name
• % B complete local month name
• % C local corresponding Date and Time Representation
• % J one day in the year (001-366)
• % P local equivalent of A. M. or P. M.
• % U number of weeks in a year (00-53) Sunday is the start of the week
• % W Week (0-6), Sunday is the beginning of the week
• % W number of weeks in a year (00-53) Monday is the start of the week
• % X local date Representation
• % X Local Time Representation
• % Z name of the current time zone
• % Itself

In Python, try:

>>> Import time >>> print (time. strftime ('% B % A') NovSun >>> print (time. strftime ('% B % A') Nov Sun >>> print (time. strftime ('% B % a % Z') Nov Sun China Standard time >>> print (time. strftime ('% B % a % p') Nov Sun PM

The above example of python simple Backup File Script v1.0 is all the content shared by Alibaba Cloud xiaobian. I hope you can give me a reference and support the help house.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.