Jason Lucas
Jason Lucas

Reputation: 113

Concatenating a Input Variable into another with strings

Trying to figure out if possible to using os.listdir on a remote PC.

I want to have user input for a computer name and then take that and use os.listdir to list a certain directory on that PC.

import os

def listdirtory():
    computername = input("What is the computer name? ")
    completepath = "\\" + computername + "\C$\\users"
    os.listdir(completepath)

listdirtory()

Where I am having trouble is I need to take out the second \ after computername and one \ after users since its reading the path with double \ like this:

FileNotFoundError: [WinError 3] The system cannot find the path specified: '\\\testmachine\\\C$\\\users'

where it would need to be \\\testmachine\C$\users\

Upvotes: 1

Views: 68

Answers (1)

blhsing
blhsing

Reputation: 106513

You should either escape a literal backslash with another backslash:

completepath = "\\\\" + computername + "\\C$\\users"

or use raw strings instead:

completepath = r"\\" + computername + r"\C$\users"

Upvotes: 3

Related Questions