Python 3.7 和 win10系統(tǒng)
首先我們需要安裝一個包,在cmd命令行界面安裝 bypy包。
pip install bypy
然后安裝成功后,在命令行運行命令
bypy info
會彈出一些類似一下的界面,要求授權系統(tǒng)認證。不過因為我已經(jīng)安裝了,所以下面是顯示我的網(wǎng)盤容量和現(xiàn)有量。具體的認證是在瀏覽器中輸入命令行界面中的一行百度云鏈接,進入授權界面,復制授權碼,重新回到命令行輸入授權碼。重新輸入bypy info 就可以認證成功了。
登錄百度網(wǎng)盤,就可以看到出現(xiàn)了下列的文件夾。由于API限制,所有文件只能存在該文件夾中。
該bypy包快速上手只要三步:創(chuàng)建對象;創(chuàng)建文件夾,上傳文件。
- # 獲取一個bypy對象,封裝了所有百度云文件操作的方法
- bp = ByPy()
- # 百度網(wǎng)盤創(chuàng)建遠程文件夾
- bp.mkdir(remotepath = 'dir_name')
- # 上傳某一文件到百度云網(wǎng)盤對應的遠程文件夾
- # ondup中參數(shù)代表復制文件,默認值為'overwrite',指定'newcopy'不會覆蓋重復文件
- bp.upload(localpath= file["fileName"], remotepath= 'dir_name', ondup='newcopy')
利用這些性質,我寫了一份代碼,功能是將代碼所在的上層文件夾以內的所有文件按原來的文件夾組織形式上傳到百度云中。也就是說,如果你要上傳一個文件夾內的所有文件,那么你可以把這份代碼拷貝到文件夾里面,然后直接運行就會自動上傳了。
代碼如下:
- from bypy import ByPy
- import os
- import time
- import datetime
- import threading
- # 百度云存放文件的文件夾名
- dir_name = "ByPy-test"
- # 獲取一個bypy對象,封裝了所有百度云文件操作的方法
- bp = ByPy()
- # 百度網(wǎng)盤創(chuàng)建遠程文件夾bypy-test
- bp.mkdir(remotepath = dir_name)
- # 函數(shù)作用:文件中的 \ 改為 /
- # 函數(shù)輸入:文件絕對路徑
- # 輸出:文件絕對路徑添加轉義符后的結果
- def changePath(filePath):
- path = ""
- for i in range(len(filePath)):
- if filePath[i] != "\\":
- path += filePath[i]
- else:
- path += "/"
- return path
- # 根據(jù)當前路徑和文件夾路徑得到相對路徑
- 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
- # 函數(shù)作用:給出文件夾,得到所有文件的絕對路徑
- # 輸入?yún)?shù):當前文件夾的絕對路徑
- # 返回值:一個包含所有文件絕對路徑,以及文件所在文件夾的大小的列表
- 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
- #獲取文件的大小,結果保留兩位小數(shù),單位為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 ("文件發(fā)送完成:本地路徑:" + 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 ("文件發(fā)送完成:" + 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")
傳輸速度還是可以的!
1. 或許可以考慮一下多線程,或者多進程發(fā)送,在文件較大較多的時候,傳輸時間會少挺多的。
2. 可以統(tǒng)計文件的修改時間,按時間段來發(fā)送文件,達到局部更新的效果。
emmm至于你說為啥放著客戶端不用,寫個鬼命令行,(⊙﹏⊙)好像也有道理哦,不過寫著玩唄。寫程序多好玩。
thx for reading.