How to copy a file from a network share to local disk with variables?

前端 未结 4 946
走了就别回头了
走了就别回头了 2021-02-02 18:21

If I use the following line:

shutil.copyfile(r\"\\\\mynetworkshare\\myfile.txt\",\"C:\\TEMP\\myfile.txt\")

everything works fine. However, what

4条回答
  •  鱼传尺愫
    2021-02-02 18:34

    The r used in your first code example is making the string a "raw" string. In this example, that means the string will see the backslashes and not try to use them to escape \\ to just \.

    To get your second code sample working, you'd use the r on the strings, and not in the copyfile command:

    source_path = r"\\mynetworkshare"
    dest_path = r"C:\TEMP"
    file_name = "\\myfile.txt"
    
    shutil.copyfile(source_path + file_name, dest_path + file_name)
    

提交回复
热议问题