1

I am getting a "WindowsError: [Error 5] Access is denied" error in my Python 2.7 script. There is no folder listed after the Access is denied message. I am not an administrator, but have full read/write/execute/modify security set for both the target .7z file and the output folder. Here is the relevant code:

if os.path.isfile(os.path.join(outRoot[0], outRoot[1] + "_photos.7z")):
    #Unzip photo folder
    import subprocess
    source = outFolder + "_photos.7z"
    pw = ''
    subprocess.call(['"C:\\Program Files\\7-Zip\\7z.exe" x ' + source + ' -o' + outRoot[0] + ' -p' + pw])
4

1 回答 1

2

You pass a list with a single entry to call(), but it should be one entry for each command line option, i.e.

subprocess.call([
    'C:\\Program Files\\7-Zip\\7z.exe',
    'x',
    source,
    '-o' + outRoot[0],
    '-p' + pw
])

The list syntax is there so you won’t have to take care of quoting yourself. Subprocess will do that for you.

What your code does is telling Python to interpret the whole command line as the name of the program to call; which, of course, will fail.

Update: Seems that 7-Zip does not like spaces after the name of an option. Updated the example code above accordingly. That’s not a Python problem, but it demonstrates nicely how the list syntax for executing a command line works. Each element in the list is treated as a single option and quoted as needed (e.g. if it contains spaces). Options get separated by whitespace.

For debugging you can pass the command list to subprocess.list2cmdline() to see the string that gets created from the list internally.

于 2016-12-15T17:17:08.010 回答