python 获取共享文件

  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	est")
    if not len(img_path):
        return False
    file_path = r"\10.101.35.241int4amize
edtag-img" + '\' + img_path[0]
    file_name = os.path.basename(file_path)
    # res = path.rsplit('.',1)[0].split('_')[1]
    res = re.search(r'(0xw+).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.1file
all_file = os.listdir(DOWNLOAD_LINK)
os.system('copy DOWNLOAD_LINK\test .\')

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