0

I am very new to python and still trying to learn the craft as best as I can on my own. I started to get into automation recently and one of the not so sexy things I'm trying to automate is the automation of Microsoft Baseline Security Analyzer. All it does is check for the patches that are on the machine and which ones are missing, simple right?

Anyways, the part that I'm stuck on is that I'm trying to execute the application to run the scan on multiple hostnames located in a text file. The command that I'm using looks like this:

subprocess.Popen(['mbsacli','/listfile','"C:\Users\me\Desktop\test.txt"', '/n', 'os+iis+sql+password'])

The part that is getting an issue is with the way python is handling the directory path. What I mean by that is that it tells me the following:

Error: Cannot open file C:\Users\me\Desktop        est.txt

The giant blank space between Desktop and "est.txt" is whats the issue, it should be test.txt first of all but its not handling the slashes() cleanly.

Any help would be very helpful, thank you.

EDIT

So far I've tried the following:

subprocess.Popen(['mbsacli','/listfile','r C:\Users\dxd0857\Desktop\test.txt', '/n', 'os+iis+sql+password'])

subprocess.Popen(['mbsacli','/listfile','C:\\Users\\dxd0857\Desktop\\test.txt', '/n', 'os+iis+sql+password'])

subprocess.Popen(['mbsacli','/listfile','r C:\\Users\\dxd0857\\Desktop\\test.txt', '/n', 'os+iis+sql+password'])

the result is still the same error:

Error: Cannot open file r C:\Users\me\Desktop      est.txt
3
  • 3
    It should be r'C:\Users\dxd0857\Desktop\test.txt'. The 'r' prefix is not part of the string. It tells the compiler how to interpret the string literal. Commented Dec 3, 2015 at 15:43
  • Thank you, this worked perfectly. I didn't realize it was supposed to be outside the the quotes. Commented Dec 3, 2015 at 15:53
  • Perhaps accept the answer then (or post one of your own and accept that) so that this question no longer comes up as unresolved. Commented Dec 3, 2015 at 17:24

1 Answer 1

3

What happens is that the \ character is parsed as special character, where \t means tab.

Therefore you have to either use \\ for backslashes:

subprocess.Popen(['mbsacli', 
                  '/listfile', 'C:\\Users\\me\\Desktop\\test.txt', 
                  '/n', 'os+iis+sql+password'])

...or use raw string instead (thanks @tripleee, @eryksun):

subprocess.Popen(['mbsacli', 
                  '/listfile', r'C:\Users\me\Desktop\test.txt', 
                  '/n', 'os+iis+sql+password'])
Sign up to request clarification or add additional context in comments.

4 Comments

The double quotes inside single quotes don't look sane, either (but then this is Windows, right?)
A somewhat more elegant solution is to use a raw string; r'C:\Users\me\Desktop\test.txt' where the r before the opening quote is significant.
@tripleee, subprocess.list2cmdline will escape the quotes when creating the command line, e.g. as r'\"C:\Users\me\Desktop\test.txt\"'. If mbsacli uses the MS C runtime, the escaped quotes will be treated as literally part of the string passed in the argv array. This is probably an error, unless mbsacli strips the quotes from its command-line argument.
Thanks for the assists everyone and I tried using both the suggestions of raw input and the \\ but it still doesn't give the correct results. The issue is still present. Very odd.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.