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

前端 未结 4 945
走了就别回头了
走了就别回头了 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:39

    This looks like an escaping issue - as balpha says, the r makes the \ character a literal, rather than a control sequence. Have you tried:

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

    (Using an interactive python session, you can see the following:

    >>> source_path = r"\\mynetworkshare"
    >>> dest_path = r"C:\TEMP"
    >>> filename = r"\my_file.txt"
    >>> print (source_path + filename)
    \\mynetworkshare\my_file.txt
    >>> print (dest_path + filename)
    C:\TEMP\my_file.txt
    

提交回复
热议问题