bypy 如何從百度雲網盤下載文件


 一、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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM