-2

So, as SO keeps suggesting me, I do not want to replace double backslashes, I want python to understand them.

I need to copy files from a windows distant directory to my local machine.

For example, a "equivalent" (even if not) in shell (with windows paths):

cp \\directory\subdirectory\file ./mylocalfile

But python does not even understand double backslashes in strings:

source = "\\dir\subdir\file"
print(source)

$ python __main__.py 
__main__.py:1: DeprecationWarning: invalid escape sequence \s
  source = "\\dir\subdir\file"
\dir\subdir
           ile

Is Python able to understand windows paths (with double backslashes) in order to perform file copies ?

Itération 122442
  • 2,644
  • 2
  • 27
  • 73

1 Answers1

0

You can try this also:

source = r"\dir\subdir\file"
print(source)
# \dir\subdir\file

You can solve this issue by using this raw string also.
What we are doing here is making "\dir\subdir\file" to raw string by using r at first.

You can visit here for some other information.

raw strings are raw string literals that treat backslash (\ ) as a literal character. For example, if we try to print a string with a “\n” inside, it will add one line break. But if we mark it as a raw string, it will simply print out the “\n” as a normal character.

imxitiz
  • 3,920
  • 3
  • 9
  • 33