首页 > 编程语言 > 详细

python 获取共享文件

时间:2021-07-13 20:29:59      阅读:17      评论:0      收藏:0      [点我收藏+]
  1. smbclient方法
def check_smb_img():
    smbclient.register_session("1.1.1.1", username="name", password="password")
    img_path = smbclient.listdir(r"\\1.1.1.1\test")
    if not len(img_path):
        return False
    file_path = r"\\10.101.35.241\int4\amize\redtag-img" + ‘\\‘ + img_path[0]
    file_name = os.path.basename(file_path)
    # res = path.rsplit(‘.‘,1)[0].split(‘_‘)[1]
    res = re.search(r‘(0x\w+)\.tar‘, file_path)
    file_checksum = res.group(1)
    return file_path, file_name, file_checksum


def read_in_chunks(filePath, chunk_size=1024 * 1024):
    file_object = smbclient.open_file(filePath, mode=‘rb‘)
    while True:
        chunk_data = file_object.read(chunk_size)
        if not chunk_data:
            break
        yield chunk_data


def verify_file_checksum(file, checksum):
    with open(file, ‘rb‘) as f:
        if zlib.adler32(f.read()) == int(checksum, 16):
            print(‘...%s downloaded successfully‘ % file)
            return True
    print(‘checksum verification fail‘)
    return False


def download_smb_file(file_path, file_name, file_checksum):
    print(‘download file:‘, file_path)
    if os.path.exists(file_name):
        os.remove(file_name)
    for chunk in read_in_chunks(file_path):
        with open(file_name, mode=‘ab‘) as fw:
            fw.write(chunk)
    verify_file_checksum(file_name, file_checksum)

  1. os直接操作
DOWNLOAD_LINK=r‘\\1.1.1.1\fileall_file = os.listdir(DOWNLOAD_LINK)
os.system(‘copy DOWNLOAD_LINK\\test .\\‘)

python 获取共享文件

原文:https://www.cnblogs.com/amize/p/15007590.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!