python文件上传与下载

从共享文件夹上下载文件到本地(仅限windows)

# -*- coding:utf-8 -*-
import os
# os.system('net use z: \\10.147.142.173\ipc$ "admin`12" /user:"F7689344"')
class CopyFile(object):
    def copyFileDir(self, srcFilename, desFilename):
        status = True
        res_list = []
        try:
            fileList = os.listdir(srcFilename)
            fileList = [img_path for img_path in fileList if
                        img_path.endswith(".jpeg") and not img_path.startswith(".")]#文件格式筛选,此处可针对文件进行修改
            for eachFile in fileList:
                sourceF = os.path.join(srcFilename, eachFile)
                targetF = os.path.join(desFilename, eachFile)
                if os.path.isdir(sourceF):
                    if not os.path.exists(targetF):
                        os.makedirs(targetF)
                    stat = self.copyFileDir(sourceF, targetF)
                    res_list.append(stat)
                else:
                    stat = self.copyFile(sourceF, targetF)
                    res_list.append(stat)
            if len(set(res_list)) > 1:
                status = False
        except Exception as e:
            status = False
        return status

    def copyFile(self, srcFilename, desFilename):
        copyCommand = 'copy' + ' "' + srcFilename + '" ' + '"' + desFilename + '"'
        try:
            stat = os.system(copyCommand)
            status = True if stat == 0 else False
        except Exception as e:
            status = False
        return status
if __name__ == '__main__':
	local_dir = "e:/ng" #本地文件夹路径
	imgDirPath = "\\\\10.147.142.164\\c_it_2\\C_series"#共享文件夹路径
	cf = CopyFile()
	if not os.path.exists(local_dir):
	    os.makedirs(local_dir)
	status = cf.copyFileDir(imgDirPath, local_dir)

fdfs_client实现文件的上传与下载

# 前提:先安装好fdfs_client
from fdfs_client.client import *
client = Fdfs_client('D:/Program_Files/Anaconda/Lib/fdfs_client/client.conf') #client.conf文件的存放位置路径
ret = client.upload_by_filename(local_filename) #上传文件到服务器
client.download_to_file(local_filename, remote_file_id) #从服务器上下载文件到本地
resultPath = ret['Remote file_id'].replace("\\", "/")#获取在文件服务器上所传文件的路径
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值