通过Python复制远程服务器下的文件到本地

该博客介绍使用Python拷贝Windows共享目录下文件的方法。先通过net use命令获取共享目录访问权限,再用xcopy命令将文件拷贝到本地目录,还给出了参考文章链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Python拷贝windown共享目录下的文件

1、先通过net use命令获取共享目录访问权限

2、使用xcopy命令进行文件拷贝到本地目录

代码如下

import os
share_user='user'
share_password='password'
share_file_host='\\\\192.168.1.24'
share_file_path='\\\\192.168.1.24\\Git\\test_share_file.txt'
target_file_path='d:\\test777'

def copy_share_file():
    permission_cmd=f'net use {share_file_host} {share_user} /user:{share_password}'
    print(permission_cmd)
    permission_result=os.popen(permission_cmd)
    print(permission_result.read())
    copy_cmd=f'xcopy /y {share_file_path} {target_file_path}'
    print(copy_cmd)
    copy_result=os.popen(copy_cmd)
    print(copy_result.read())

if __name__ == '__main__':
    copy_share_file()

参考文章:

https://blog.csdn.net/Gretchen_Grundler/article/details/79458770

https://www.cnblogs.com/lx823706/p/5976546.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值