Python Forum
extra slashes in the network path for shutil.copy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
extra slashes in the network path for shutil.copy
#1
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.
Reply
#2
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!
Reply
#3
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
Reply
#4
(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.
tester_V likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  use of shutil.copytree with ENOTDIR exception yan 2 839 Nov-29-2023, 03:02 PM
Last Post: yan
  shutil.move make data corrupt kucingkembar 0 742 Feb-01-2023, 01:30 PM
Last Post: kucingkembar
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 5 1,492 Aug-28-2022, 07:11 AM
Last Post: Melcu54
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,148 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Extra slashes in path tester_V 3 2,234 Feb-13-2021, 09:38 PM
Last Post: tester_V
  concatenat - shutil jmabrito 3 2,142 Feb-11-2021, 12:17 PM
Last Post: jmabrito
  Failing to copy file from a network to a local drive tester_V 4 6,972 Jan-20-2021, 07:40 AM
Last Post: tester_V
  Shutil FileNotFoundError: Errno 2 Help lord_kaiser 8 10,356 Aug-10-2020, 08:45 AM
Last Post: lord_kaiser
  shutil.copy questions kristianpython 3 2,251 Jul-14-2020, 09:19 AM
Last Post: Gribouillis
  Shutil move if file exists in destination Friend 2 6,641 Feb-02-2020, 01:45 PM
Last Post: Friend

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020