11--python Backup File Program

Source: Internet
Author: User
Tags rar

Recently read the next "a Byte of Python", see a very interesting program, using Python for file Line Backup practice program, the machine to hit the code to run once, resulting in a small problem, the path error-"No compression software found", Online search answers, Found that a lot of people use Python to achieve this function, and are original, happy, the heart is very admire them.

But after a closer look, it turns out that many of these programs are exactly the same as in a Byte of Python, with some progressive points and more print cues. The actual operation is a variety of problems. Suddenly feel some no words, so-called original, incredibly is this way. I think this should be a common problem in the domestic network, so there are problems or more to the foreign forum exchange is very good, such as Stackoverfolow.

I'm going to put on my own code:

1 ImportOS2 Import Time3 4 #1. Folders or files that need to be backed up5Source = ['D:\\backup']6 #2. Storage directory that needs to be backed up7Target_dir ='d:\\work\\'8 #3. To compress the files, on Windows via WinRAR9target = Target_dir +time.strftime ('%y%m%d%h%m%s') +'. 7z'Ten #4. Compress package with WinRAR OneZip_command ='rar A%s%s'% (target,"'. Join (source))//Use the Join () method to stitch the string A #5. Run the script for backup - ifOs.system (zip_command) = =0: -     Print 'successful backuped', Target the Else: -     Print "Backup Failed"

In this program I met the main problem is that the Windows platform does not want to bring the Linux zip command, so we need to install the decompression software set environment variables before the normal operation of the above backup program.

can install 7zip or winrar can be, just need to replace the command line parameters, if using 7zip then 11 lines to change to 7z if the WinRAR is changed to RAR.

Remember to add the compression software environment variables, turn off CMD to reopen the next cmd, as for why, say it is tears!

Attach the Join () method used in the program:

Join () function

Syntax: ' Sep '. Join (SEQ)

Parameter description
Sep: Delimiter. Can be empty '
SEQ: sequence of elements to concatenate, strings, tuples, dictionaries
The syntax above is to combine all the SEQ elements into a new string with Sep as a delimiter

Return value: Returns a string that is generated after each element is concatenated with the delimiter Sep

>>> seq = [' Hello ', ' good ', ' boy ']
>>> print '. Join (SEQ)
>>> print '. Join (SEQ)
Hello Good boy

11--python Backup File Program

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.