bloodstorm17
bloodstorm17

Reputation: 521

Python Subprocess POpen Directory Handling

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

Upvotes: 0

Views: 112

Answers (1)

plaes
plaes

Reputation: 32746

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'])

Upvotes: 3

Related Questions