cicofuente
cicofuente

Reputation: 1

variable inside a subprocess.call in python

I want to insert a variable in place of the ip address this is my working code without the variable:

subprocess.call(r'net use T: \\192.168.1.10\myshare /PERSISTENT:YES', shell=True)

this is my not workings attempts with the variable:

myip = "192.168.1.10"
subprocess.call(r'net use T: \\'+myip'\myshare /PERSISTENT:YES', shell=True)

#or:
subprocess.call(r'net use T: \\', 'myip', '\myshare /PERSISTENT:YES', shell=True)

#or:
subprocess.call(r'net use T: \\'+myip+'\myshare /PERSISTENT:YES', shell=True)

#or:
subprocess.call(r'net use T: \\+myip+\myshare /PERSISTENT:YES', shell=True)

none of these works any suggestions?

Upvotes: 0

Views: 62

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54698

You need the raw prefix on any substring that has backslashes:

subprocess.call(r'net use T: \\'+myip+r'\myshare /PERSISTENT:YES', shell=True)

Or:

subprocess.call(
   ['net', 'use', rf'\\{myip}\myshare', '/PERSISTENT:YES'],
   shell=True
)

Upvotes: 1

Related Questions