Python Forum

Full Version: Extra slashes in path
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
This is strange, Confused I have extra slashes in a 'path'.
I do not understand how it could happen. Undecided
I have a Host list, I'm iterating over it and adding a "path" to each name,
I'm going to download files from later on.
The host list goes like this:

CM04TMMV0001EK
CM04TMMV0002EK

Code
lst = 'path/to/the/list.txt' 
with open (lst,'r') as lr :
    for itm in lr :
        itm=itm.strip()
        d_to_sacn =r"\\"+itm+"\c$\SBHI\Debug_Logs" 
        print ("whole string -- ",d_to_sacn)
here is a print out, I have a two extra lines at the bottom :
whole string -- \\CM04TMMV0001EK\c$\SBHI\Debug_Logs
whole string -- \\CM04TMMV0002EK\c$\SBHI\Debug_Logs
whole string -- \\\c$\SBHI\Debug_Logs
whole string -- \\\c$\SBHI\Debug_Logs
I believe host is "\\" in the last two lines.
Add repr() and you see all👀
lst = 'list.txt'
with open (lst, 'r') as lr:
    for itm in lr :
        itm = itm.strip()
        print(repr(itm))
        d_to_sacn = fr"\\{itm}\c$\SBHI\Debug_Logs"
        print (f"whole string -- {d_to_sacn}")
Output:
'CM04TMMV0001EK' whole string -- \\CM04TMMV0001EK\c$\SBHI\Debug_Logs 'CM04TMMV0002EK' whole string -- \\CM04TMMV0002EK\c$\SBHI\Debug_Logs
Adding one \ in last \CM04TMMV0002EK
Output:
'CM04TMMV0001EK' whole string -- \\CM04TMMV0001EK\c$\SBHI\Debug_Logs '\\CM04TMMV0002EK' whole string -- \\\CM04TMMV0002EK\c$\SBHI\Debug_Logs
Thank you!