If I use the following line:
shutil.copyfile(r\"\\\\mynetworkshare\\myfile.txt\",\"C:\\TEMP\\myfile.txt\")
everything works fine. However, what
From your example paths, it's clear that we are discussing the Windows OS
.
Python implementation on this OS use a common (C
) runtime library that accepts forward slashes as equivalent to back-slashes. This way you can avoid escape char issues.
source_path = "//mynetworkshare"
dest_path = "C:/TEMP"
file_name = "/myfile.txt"
Note that filename composition is handled by os.path.join:
Join one or more path components intelligently. If any component is an absolute path, all previous components (on Windows, including the previous drive letter, if there was one) are thrown away, and joining continues. The return value is the concatenation of path1, and optionally path2, etc., with exactly one directory separator (os.sep) inserted between components, unless path2 is empty. Note that on Windows, since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.
import os
shutil.copyfile(os.path.join(source_path, file_name),
os.path.join(dest_path, file_name))