Running system command in python script

I go through the Python Byte to find out the syntax and methods, etc.

I just started with a simple backup script (right from the book):

#!/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 = ['"C:\\My Documents"', 'C:\\Code'] # Notice we had to use double quotes inside the string for names with spaces in it. # 2. The backup must be stored in a main backup directory target_dir = '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 + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip' # 5. We use the zip command to put the files in a zip archive zip_command = "zip -qr {0} {1}".format(target, ' '.join(source)) # Run the backup if os.system(zip_command) == 0: print('Successful backup to', target) else: print('Backup FAILED') 

That's right, he fails. If I run the zip command in the terminal, it works fine. I think this fails because zip_command never starts. And I don’t know how to run it.

Just typing zip_command doesn't work. (I am using python 3.1)

+4
source share
4 answers

This will help us if you can format the code as a code; select parts of the code and click the "Sample Code" button on the editor toolbar. The icon looks like this: "101/010", and if you hold the mouse pointer over it, the yellow field "hint" says "Example code <pre> </pre> Ctrl + K"

I just tried, and if you paste the code into the StackOverflow editor, the lines with '#' will be in bold. Therefore, bold lines are comments. So far so good.

Your lines seem to contain backslash characters. You will need to double each backslash, for example:

 target_dir = 'E:\\Backup' 

This is because Python handles backslashes specifically. It introduces a "backslash", which allows you to put a quote inside a quoted string:

 single_quote = '\'' 

You can also use the raw string of Python, which has much simpler backslash rules. The raw string is entered r" or r' and ends with " or ' respectively. examples:

 # both of these are legal target_dir = r"E:\Backup" target_dir = r'E:\Backup' 
+1
source

Are you sure that the Python script sees the same environment that you have access to when you enter the command manually in the shell? Maybe zip is not in the way when Python runs the command.

+1
source

The next step that I recommend is changing your script to print the command line, and just look at the line and see if this seems correct.

Another thing you can try is to create a batch file that prints environment variables and run Python and see what the environment looks like. Especially PATH.

Here is an example:

 set echo Trying to run zip... zip 

Put them in a batch file named C:\mytest.cmd , and then run its Python code:

 result_code = os.system("C:\\mytest.cmd") print('Result of running mytest was code', result_code) 

If this works, you will see that the environment variables are printed, then it will echo "Trying to start zip ...", and then if zip starts, it will display a message with the zip version number and how to start it.

0
source

The zip command only works on Linux, not on Windows. That is why she makes a mistake.

0
source

All Articles