I'm trying to connect another computer in local network via python (subprocesses module) with this commands from CMD.exe
net use \\\\ip\C$ password /user:username
copy D:\file.txt \\ip\C$
Then in python it look like below. But when i try second command, I get:
"FileNotFoundError: [WinError 2]"
Have you met same problem? Is there any way to fix it?
import subprocess as sp
code = sp.call(r'net use \\<ip>\C$ <pass> /user:<username>')
print(code)
sp.call(r'copy D:\file.txt \\<ip>\C$')
The issue is that copy
is a built-in, not a real command in Windows.
Those Windows messages are awful, but "FileNotFoundError: [WinError 2]"
doesn't mean one of source & destination files can't be accessed (if copy
failed, you'd get a normal Windows message with explicit file names).
Here, it means that the command could not be accessed.
So you'd need to add shell=True
to your subprocess call to gain access to built-ins.
But don't do that (security issues, non-portability), use shutil.copy
instead.
Aside, use check_call
instead of call
for your first command, as if net use
fails, the rest will fail too. Better have an early failure.
To sum it up, here's what I would do:
import shutil
import subprocess as sp
sp.check_call(['net','use',r'\\<ip>\C$','password','/user:<username>'])
shutil.copy(r'D:\file.txt,r'\\<ip>\C$')