0

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?

2
  • Your third example (subprocess.call(r'net use T: \\'+myip+'\myshare /PERSISTENT:YES', shell=True)) should have worked. Did you get any error message? Commented Jul 19, 2022 at 20:25
  • The second substring also has backslashes and needs the r prefix. Commented Jul 19, 2022 at 20:27

1 Answer 1

1

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
)
Sign up to request clarification or add additional context in comments.

Comments

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.