Parse the correct operation method when Python calls the zip command

Source: Internet
Author: User
Tags natural string

When we use the Python programming language for program development, we will find that this language can help us easily complete some specific functional requirements. Here we will first learn how to use Python to call the zip command, so as to understand the operation methods of this language.

Python uses the following example to call the zip command:

 
 
  1. #!/usr/bin/Python  
  2. # Filename: backup_ver1.py  
  3. import os  
  4. import time  
  5. # 1. The files and directories to be backed up are specified in a list.  
  6. source = ['/home/swaroop/byte', '/home/swaroop/bin']  
  7. # If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] 
    or something like that  
  8. # 2. The backup must be stored in a main backup directory  
  9. target_dir = '/mnt/e/backup/' # Remember to change this to what 
    you will be using  
  10. # 3. The files are backed up into a zip file.  
  11. # 4. The name of the zip archive is the current date and time  
  12. target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'  
  13. # 5. We use the zip command (in Unix/Linux) to put the files 
    in a zip archive  
  14. zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))  
  15. # Run the backup  
  16. if os.system(zip_command) == 0:  
  17. print 'Successful backup to', target  
  18. else:  
  19. print 'Backup FAILED' 

Since the above example of calling the zip command in Python is in Unix/Linux, you need to change it to windows

 
 
  1. #!/usr/bin/Python  
  2. # Filename: backup_ver1.py  
  3. import os  
  4. import time  
  5. source =[r'C:\My Documents', r'D:\Work']  
  6. target_dir = r'F:\back up\' # Remember to change this to 
    what you will be using  
  7. target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'  
  8. zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))  
  9. # Run the backup  
  10. if os.system(zip_command) == 0:  
  11. print 'Successful backup to', target  
  12. else:  
  13. print 'Backup FAILED' 

Question 1:

After the code is changed, an exception occurs during running. The message "EOL while scanning single-quoted string" appears in the bold line of the above Code.

 
 
  1. target_dir = r'F:\back up\' 

In Python, an error occurs when calling the zip command mainly because of the problem between the Escape Character and the natural symbol string. For details, refer to the Python introduction:

  • How to Implement tab file operations in Python
  • Use Python recursion to process objects
  • Analysis of simple Python File Operations
  • Analysis of the meaning and application characteristics of the Python numeric type
  • Summary of basic types of Python Operators

Natural string

If you want to indicate certain strings that do not require special processing such as escape characters, you need to specify a natural string. A natural string is specified by adding the prefix r or R to the string. For example, r "Newlines are indicated by/n ".

As mentioned above, the value of target_dir should be treated as 'f: \ back up \ ', but the escape character here is processed. If you replace it with the r'f: \ back up \ 'escape character but it is not processed, the value of target_dir will be changed to 'f: \ back up \\'. convert single quotes into double quotes. If you add brackets to it and change it to r 'f: \ back up \ ', then the program will be okay...

Therefore, there are two solutions: 1) As mentioned above, enclose the brackets; 2) without the prefix r, use the Escape Character '\' directly and define it as target_dir = 'f: \ back up \\'.

Question 2:

After problem 1 is solved, run module and prompt backup fail. Check as follows:

1. Print the source and target strings to check whether the file path is incorrect.

2. If you suspect that windows does not have a zip command, you can press 'zip' in the command line, but a prompt is displayed for help, proving that you can use the zip command with the q, r parameter;

3. Think of sqlplus commands that do not accept space characters, so try to replace the file name with no space, and the module runs successfully...

Now, the question is how to make the zip command accept the path with spaces. After google's answer, the following message is displayed: "wildcards with spaces or file names must be enclosed by quotation marks"

Then, modify zip_command

Zip_command = "zip-qr '% s' % s" % (target, ''. join (source ))

Changed:

Zip_command = "zip-qr \" % s \ "\" % s \ "" % (target, '\ "\"'. join (source ))

After modification, the module runs successfully...

The correct script should be:

 
 
  1. #!/usr/bin/Python  
  2. # Filename: backup_ver1.py  
  3. import os  
  4. import time  
  5. source =[r'C:\My Documents', r'D:\Work']  
  6. target_dir = 'F:\\back up\\' # Remember to change this to what 
    you will be using  
  7. target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'  
  8. zip_command = "zip -qr \"%s\" \"%s\"" % (target, ' '.join(source))  
  9. # Run the backup  
  10. if os.system(zip_command) == 0:  
  11. print 'Successful backup to', target  
  12. else:  
  13. print 'Backup FAILED' 

The preceding section describes how to use Python to call the zip command.

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.