简体   繁体   中英

python - backward slash is removed while referring to path in network on windows

netowrk_path = '\\shared_storage\test.txt'
cmd = f'/c copy {netowrk_path} D:\\temp'
print(cmd)

this prints and gives error while copying

/c copy \shared_storage test.txt D:\temp

but it should actually print

/c copy \\shared_storage test.txt D:\temp

backslash(\) is a way to escape certain characters learn more

you can define the strings as raw strings

string = r'\\somenetworkpath'
print(string)

output: \\somenetworkpath

read more about the raw string

You can use repr in the following way:

print(repr(cmd))

This will give you the desired output

You can use raw string and format in the following way:

netowrk_path = r'\\shared_storage\test.txt'
cmd = rf'/c copy {netowrk_path} D:\\temp'
print(cmd)

You can replace all single \ with double slash \\ or make it a raw string using r as suggested above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM