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