How to copy files to network path or drive using Python

前端 未结 2 1881
小鲜肉
小鲜肉 2020-12-16 04:33

Mine is similar to this question.

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

The only difference is my network drive has a passwor

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-16 05:07

    If you have the pywin32 library (eg comes part of the ActiveState Python distro), then you can get it done in a few lines, without mapping a drive:

    import win32wnet
    win32wnet.WNetAddConnection2(0, None, '\\\\'+host, None, username, password)
    shutil.copy(source_file, '\\\\'+host+dest_share_path+'\\')
    win32wnet.WNetCancelConnection2('\\\\'+host, 0, 0) # optional disconnect
    

    There is a more complete example on ActiveState Code

提交回复
热议问题