1

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\

1 Answer 1

3

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

1 Comment

If you want it to work on all OS, you should use os.path.join() instead of concatenating with +.

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.