简体   繁体   中英

running a system command in a python script

I have been going through "A byte of Python" to learn the syntax and methods etc...

I have just started with a simple backup script (straight 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')

Right, it fails. If I run the zip command in the terminal it works fine. I think it fails because the zip_command is never actually run. And I don't know how to run it.

Simply typing out zip_command does not work. (I am using python 3.1)

It would help us if you could format your code as code; select the code parts, and click on the "Code Sample" button in the editor toolbar. The icon looks like "101/010" and if you hold the mouse pointer over it, the yellow "tool tip" box says "Code Sample <pre></pre> Ctrl+K"

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

Your strings seem to contain backslash characters. You will need to double each backslash, like so:

target_dir = 'E:\\Backup'

This is because Python treats the backslash specially. It introduces a "backslash escape", which lets you put a quote inside a quoted string:

single_quote = '\''

You could also use a Python "raw string", which has much simpler rules for a backslash. A raw string is introduced by r" or r' and terminated by " or ' respectively. examples:

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

Are you sure that the Python script is seeing the same environment you have access to when you enter the command manually in the shell? It could be that zip isn't on the path when Python launches the command.

The next step I recommend is to modify your script to print the command string, and just look at the string and see if it seems correct.

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

Here is a suggested example:

set
echo Trying to run zip...
zip

Put those in a batch file called C:\\mytest.cmd , and then have your Python code run it:

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

If it works, you will see the environment variables printed out, then it will echo "Trying to run zip...", then if zip runs it will print a message with the version number of zip and how to run it.

zip命令仅适用于Linux,不适用于Windows。这就是为什么它会出错。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM