Good afternoon, I ran into such a problem. You need to execute a command from the Python script:

copy \\example.com\lvl0\lvl2\lvl3 Windows 

I am writing this code:

 path = r'copy \\example.com\lvl0\lvl2\lvl3 Windows' path = path.split() p2 = Popen(path, stdout=PIPE) 

As a result, the path variable is:

 ['copy', '\\\\example.com\\lvl0\\lvl2\\lvl3', 'Windows'] 

And the Popen command does not work as it should, because it accepts an incorrect path with duplicated slashes. Tell me, please, how to pass the path in these cases?

Tried to solve this problem like this:

 path = [] path.append[r'copy'] path.append[r'\\example.com\lvl0\lvl2\lvl3'] path.append[r'Windows'] p2 = Popen(path, stdout=PIPE) 
  • You are mistaken, you have everything in order with slashes. Look for the problem in another. - Pavel Mayorov
  • '\\' is one character, not two. - Pavel Mayorov
  • aside: do not use stdout=PIPE if you do not read from p2.stdout - jfs

2 answers 2

In fact, your problem is that there is no such program as copy . copy is a command of the command processor, and to execute it, you need to start the command processor itself:

 path = ['cmd', '/c', 'copy', r'\\example.com\lvl0\lvl2\lvl3', 'Windows'] 
  • It all worked. Thank! - Dmitry
 #!/usr/bin/env python3 import subprocess subprocess.check_call(r'copy \\example.com\lvl0\lvl2\lvl3 Windows', shell=True, stdout=subprocess.DEVNULL) 
  • shell=True necessary, since copy is the "internal command" , which is started from under cmd.exe
  • stdout=DEVNULL suppresses standard command output
  • subprocess.check_call() runs the command, waits for it to return and throws an exception if the command returned a non-zero return code (indicates an error).