Python Forum
extra slashes in the network path for shutil.copy - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: extra slashes in the network path for shutil.copy (/thread-33849.html)



extra slashes in the network path for shutil.copy - tester_V - Jun-01-2021

Hi,
I'm trying to copy logs from a network drive.
There is a text file it holds all the Path\file lines for files copy, like this:
\\\\nhkBCN1376\\d$\\logs\\Nhk\\ServiceHostLog.1.xml
\\\\nhkBCN1376\\d$\\logs\\Nhk\\ServiceHostLog.2.xml
...
\\\\nhkBCN1376\\d$\\logs\\Nhk\\ServiceHostLog.203.xml
I read the file and each line becomes my 'source' string.
Then I build a 'destination' veriable :
"D:\\ServiceHostLog\\"+machine_Name+"\\"+machine_Name+".xml"
I print the 'source' and the 'destination' to make sure both Vars are ok.
And they look ok.
But when I run 'shutil.copy' it produces an error:
No such file or directory: '\\\\\\\\nhkBCN1376\\\\d$\\\\logs\\\\Nhk\\\\ServiceHostLog.1.xml'
I got extra slashes introduced into the 'source' string
I have never seen it before...
thank you.


RE: extra slashes in the network path for shutil.copy - tester_V - Jun-02-2021

Hi,
I got an update for those that are interested,
I used 'replace' :
sp2_lntod=sp2_lntod.replace("\\\\","/")
it fixed the problem and I'm copying files, it just very odd, why, and how I got extra slashes...
Thank you!


RE: extra slashes in the network path for shutil.copy - deanhystad - Jun-02-2021

Because backslash is used to start an escape sequence, special characters that don't have a single ascii character (\n for example). To prevent treating the backslash this way you preceed it with a backslash. The functions you called added extra backslashes to retain the backslashes already there. This makes backslash an unfortunate choice as a separator in a file path.

Much confusion can be avoided by using pathlib calls instead of os calls


RE: extra slashes in the network path for shutil.copy - supuflounder - Jun-02-2021

(Jun-01-2021, 06:51 PM)tester_V Wrote: Hi,
I'm trying to copy logs from a network drive.
There is a text file it holds all the Path\file lines for files copy, like this:
\\\\nhkBCN1376\\d$\\logs\\Nhk\\ServiceHostLog.1.xml
\\\\nhkBCN1376\\d$\\logs\\Nhk\\ServiceHostLog.2.xml
...
\\\\nhkBCN1376\\d$\\logs\\Nhk\\ServiceHostLog.203.xml
I read the file and each line becomes my 'source' string.
Then I build a 'destination' veriable :
"D:\\ServiceHostLog\\"+machine_Name+"\\"+machine_Name+".xml"
I print the 'source' and the 'destination' to make sure both Vars are ok.
And they look ok.
But when I run 'shutil.copy' it produces an error:
No such file or directory: '\\\\\\\\nhkBCN1376\\\\d$\\\\logs\\\\Nhk\\\\ServiceHostLog.1.xml'
I got extra slashes introduced into the 'source' string
I have never seen it before...
thank you.

If you meant to say \\nhkBCN1376...
why did you write \\\\nhkBCN1376...

The doubling of backslashes applies only to strings in your source code. If you read text from a file, it comes in verbatim.