-2

I want to access some files in my network drive. My network drive is called "networkfile". If I just run this on the Window command line, it is working: net use \networkfile\Programs.

However, It didn't work when I put it in the Python script (I'm using Python3). I tried:

a = os.system("net use O:\networkfile\Programs")

a = os.system("net use \networkfile\Programs")

a = os.system("net use \networkfile\Programs")

a = subprocess.run("net use O:\networkfile\Programs", shell=True, stdout=subprocess.PIPE)

None of those work. The error is: "System error 67 has occurred. The network name cannot be found."

Anyone has experienced this before? Please advice.

Thanks,

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
cs_wind
  • 25
  • 4
  • 1
    `print("net use \networkfile\Programs")` and see what it shows you... – juanpa.arrivillaga Nov 28 '17 at 18:25
  • @juanpa.arrivillaga if it were just escaping, the third tiral would have solved the problem would it not? – Aaron Nov 28 '17 at 18:26
  • 1
    @Aaron um, no, **you** seem to have edited in the escaping backslashes, changing the question completely... – juanpa.arrivillaga Nov 28 '17 at 18:29
  • @juanpa.arrivillaga my bad... I just hit the auto format as code tool. I didn't know it auto inserted backslashes – Aaron Nov 28 '17 at 18:30
  • if command in Windows starts with `\\n` then in Python you have to use `\\\\n`. See `print('\\\\n', '\\n')` Or use raw string with prefix `r` - `print(r'\\n')` – furas Nov 28 '17 at 18:31
  • @Aaron it's alright, I see what you mean. OP needs to show exactly what they are using... – juanpa.arrivillaga Nov 28 '17 at 18:32
  • @juanpa.arrivillaga that seems to me like a bug in the editor... It was my understanding that it simply indented any highlighted text four spaces with no other changes. I had no intention of changing the text in that way – Aaron Nov 28 '17 at 18:33
  • @Aaron i think it's just markdown. – juanpa.arrivillaga Nov 28 '17 at 18:34
  • The double flash works. This works for me: a = os.system("net use \\\networkfile\\Programs") . Thank you for all your help. I appreciate it. – cs_wind Nov 29 '17 at 19:25

1 Answers1

0

your string "net use O:\networkfile\Programs" is being evaluated by the python interpreter as:

net use O:
etworkfile\Programs

because the \n is interpreted as a newline character. You can work around this in a couple different ways

  1. use a raw string (see four paragraphs down here) to prevent backslashes being treated specially (in most cases).

  2. escape the backslashes themselves so they evaluate to a literal backslash (make all "\" into "\\")

  3. use the os.path library to generate the string so the correct directory separator is used regardless of operating system.

Aaron
  • 10,133
  • 1
  • 24
  • 40
  • The double flash works. This works for me: a = os.system("net use \\\networkfile\\Programs") . Thank you for all your help. I appreciate it. – cs_wind Nov 29 '17 at 19:53