一、bypy 如何从百度云网盘下载文件
1、安装pypy
[root@ansible-server ~]# pip install bypy
2、输入bypy info 进行认证
复制这代代码在浏览器中打开
然后复制授权码--->再按回车。
3、需要在百度网盘上面,复制到bypy文件夹中。
我的应用数据--->bypy--->需要下载的文件 (备注:把你要下载的文件,先放到bypy文件夹中,就可以下载。)
4、下载文件
[root@ansible-server ~]# bypy list /apps/bypy ($t $f $s $m $d): F docker-k8s-devops-master.zip 2265427 2019-05-30, 16:34:55 5346064f4bd85c934af37b91551f8b6c #下载文件 [root@ansible-server ~]# bypy downfile docker-k8s-devops-master.zip #查看文件 [root@ansible-server ~]# ll -rw-r--r--. 1 root root 2265427 May 30 16:36 docker-k8s-devops-master.zip
5、上传文件
#需要上传的文件 [root@ansible-server ~]# ll -rw-r--r--. 1 root root 8507412 Apr 23 2017 wordpress-4.7.4-zh_CN.tar.gz #上传文件 [root@ansible-server ~]# bypy upload wordpress-4.7.4-zh_CN.tar.gz
查看结果:
刷新一下百度网盘页面
6、通过程序上传文件
[root@ansible-server ~]# pip3 install bypy
[root@ansible-server ~]# ll
total 511684
-rw-r--r--. 1 root root 3167 May 30 17:06 bypy_upload_file.py
#上传代码,具于Python3编写的。
[root@ansible-server ~]# cat bypy_upload_file.py
from bypy import ByPy import os import time import datetime import threading # 百度云存放文件的文件夹名 dir_name = "ByPy-test" # 获取一个bypy对象,封装了所有百度云文件操作的方法 bp = ByPy() # 百度网盘创建远程文件夹bypy-test bp.mkdir(remotepath = dir_name) # 函数作用:文件中的 \ 改为 / # 函数输入:文件绝对路径 # 输出:文件绝对路径添加转义符后的结果 def changePath(filePath): path = "" for i in range(len(filePath)): if filePath[i] != "\\": path += filePath[i] else: path += "/" return path # 根据当前路径和文件夹路径得到相对路径 def relPath(filePath, topDir): relativepath = "" for i in range(len(filePath)): if i < len(topDir) and filePath[i] == topDir[i]: continue relativepath += filePath[i] #print ("相对路径" + relativepath) return relativepath # 函数作用:给出文件夹,得到所有文件的绝对路径 # 输入参数:当前文件夹的绝对路径 # 返回值:一个包含所有文件绝对路径,以及文件所在文件夹的大小的列表 def getFileList(file_dir): fileList = [] top_dir = "" checkFlag = False for root, dirs, files in os.walk(file_dir): #print(root) #当前目录路径 if checkFlag == False: top_dir = root checkFlag = True #print(dirs) #当前路径下所有子目录 #print(files) #当前路径下所有非目录子文件 for file in files: fileDict = dict(Path = changePath(relPath(root, top_dir)), fileName = file, createFlag = False) fileList.append(fileDict) # 当前目录+文件名 #print(fileDict) return fileList #获取文件的大小,结果保留两位小数,单位为MB def get_FileSize(filePath): fsize = os.path.getsize(filePath) fsize = fsize/float(1024*1024) return round(fsize,2) # 获取文件绝对路径列表 allFiles = getFileList(os.path.abspath('.')) totalFileSize = 0 # 文件大小变量 start = datetime.datetime.now() # 计时开始 # 逐个上传 createFlag = {} for file in allFiles: #bp.upload(localpath=file, remotepath=dir_name, ondup='newcopy') print("正在上传文件:" + file["fileName"]) if file["Path"] != "": bp.mkdir(remotepath = dir_name + file["Path"]) DIR_NAME = dir_name + file["Path"] bp.upload(localpath= "." + file["Path"]+ "/" +file["fileName"], remotepath = str(DIR_NAME), ondup='newcopy') print ("文件发送完成:本地路径:" + file["Path"]+"/" +file["fileName"] + " 远程文件夹:" + DIR_NAME) totalFileSize += get_FileSize( "." + file["Path"]+ "/" +file["fileName"]) else: bp.upload(localpath= file["fileName"], remotepath= dir_name, ondup='newcopy') print ("文件发送完成:" + file["fileName"] + " 远程文件夹:" + dir_name) totalFileSize += get_FileSize( "." + file["Path"]+ "/" +file["fileName"]) print ("------------------------------------") end = datetime.datetime.now() # 计时结束 print("上传文件总大小为" + str(totalFileSize) + "MB") print("花费时间(s):" + str((end - start).seconds)) print("\nupload ok")
#授权并运行
[root@ansible-server ~]# chmod +x bypy_upload_file.py
[root@ansible-server ~]# python3 bypy_upload_file.py