乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      10分鐘內(nèi)教你用Python實(shí)現(xiàn)多個(gè)文件自動(dòng)上傳到百度云

       horkss 2019-12-11

      一、環(huán)境說明

      Python 3.7  和 win10系統(tǒng)

       

      二、準(zhǔn)備工作

      首先我們需要安裝一個(gè)包,在cmd命令行界面安裝 bypy包。

      pip install bypy

      然后安裝成功后,在命令行運(yùn)行命令

      bypy info

      會彈出一些類似一下的界面,要求授權(quán)系統(tǒng)認(rèn)證。不過因?yàn)槲乙呀?jīng)安裝了,所以下面是顯示我的網(wǎng)盤容量和現(xiàn)有量。具體的認(rèn)證是在瀏覽器中輸入命令行界面中的一行百度云鏈接,進(jìn)入授權(quán)界面,復(fù)制授權(quán)碼,重新回到命令行輸入授權(quán)碼。重新輸入bypy info 就可以認(rèn)證成功了。

      登錄百度網(wǎng)盤,就可以看到出現(xiàn)了下列的文件夾。由于API限制,所有文件只能存在該文件夾中。

       

      三、代碼部分

      該bypy包快速上手只要三步:創(chuàng)建對象;創(chuàng)建文件夾,上傳文件。

      1. # 獲取一個(gè)bypy對象,封裝了所有百度云文件操作的方法
      2. bp = ByPy()
      3. # 百度網(wǎng)盤創(chuàng)建遠(yuǎn)程文件夾
      4. bp.mkdir(remotepath = 'dir_name')
      5. # 上傳某一文件到百度云網(wǎng)盤對應(yīng)的遠(yuǎn)程文件夾
      6. # ondup中參數(shù)代表復(fù)制文件,默認(rèn)值為'overwrite',指定'newcopy'不會覆蓋重復(fù)文件
      7. bp.upload(localpath= file["fileName"], remotepath= 'dir_name', ondup='newcopy')

      利用這些性質(zhì),我寫了一份代碼,功能是將代碼所在的上層文件夾以內(nèi)的所有文件按原來的文件夾組織形式上傳到百度云中。也就是說,如果你要上傳一個(gè)文件夾內(nèi)的所有文件,那么你可以把這份代碼拷貝到文件夾里面,然后直接運(yùn)行就會自動(dòng)上傳了。

      代碼如下:

      1. from bypy import ByPy
      2. import os
      3. import time
      4. import datetime
      5. import threading
      6. # 百度云存放文件的文件夾名
      7. dir_name = "ByPy-test"
      8. # 獲取一個(gè)bypy對象,封裝了所有百度云文件操作的方法
      9. bp = ByPy()
      10. # 百度網(wǎng)盤創(chuàng)建遠(yuǎn)程文件夾bypy-test
      11. bp.mkdir(remotepath = dir_name)
      12. # 函數(shù)作用:文件中的 \ 改為 /
      13. # 函數(shù)輸入:文件絕對路徑
      14. # 輸出:文件絕對路徑添加轉(zhuǎn)義符后的結(jié)果
      15. def changePath(filePath):
      16. path = ""
      17. for i in range(len(filePath)):
      18. if filePath[i] != "\\":
      19. path += filePath[i]
      20. else:
      21. path += "/"
      22. return path
      23. # 根據(jù)當(dāng)前路徑和文件夾路徑得到相對路徑
      24. def relPath(filePath, topDir):
      25. relativepath = ""
      26. for i in range(len(filePath)):
      27. if i < len(topDir) and filePath[i] == topDir[i]:
      28. continue
      29. relativepath += filePath[i]
      30. #print ("相對路徑" + relativepath)
      31. return relativepath
      32. # 函數(shù)作用:給出文件夾,得到所有文件的絕對路徑
      33. # 輸入?yún)?shù):當(dāng)前文件夾的絕對路徑
      34. # 返回值:一個(gè)包含所有文件絕對路徑,以及文件所在文件夾的大小的列表
      35. def getFileList(file_dir):
      36. fileList = []
      37. top_dir = ""
      38. checkFlag = False
      39. for root, dirs, files in os.walk(file_dir):
      40. #print(root) #當(dāng)前目錄路徑
      41. if checkFlag == False:
      42. top_dir = root
      43. checkFlag = True
      44. #print(dirs) #當(dāng)前路徑下所有子目錄
      45. #print(files) #當(dāng)前路徑下所有非目錄子文件
      46. for file in files:
      47. fileDict = dict(Path = changePath(relPath(root, top_dir)), fileName = file, createFlag = False)
      48. fileList.append(fileDict) # 當(dāng)前目錄+文件名
      49. #print(fileDict)
      50. return fileList
      51. #獲取文件的大小,結(jié)果保留兩位小數(shù),單位為MB
      52. def get_FileSize(filePath):
      53. fsize = os.path.getsize(filePath)
      54. fsize = fsize/float(1024*1024)
      55. return round(fsize,2)
      56. # 獲取文件絕對路徑列表
      57. allFiles = getFileList(os.path.abspath('.'))
      58. totalFileSize = 0 # 文件大小變量
      59. start = datetime.datetime.now() # 計(jì)時(shí)開始
      60. # 逐個(gè)上傳
      61. createFlag = {}
      62. for file in allFiles:
      63. #bp.upload(localpath=file, remotepath=dir_name, ondup='newcopy')
      64. print("正在上傳文件:" + file["fileName"])
      65. if file["Path"] != "":
      66. bp.mkdir(remotepath = dir_name + file["Path"])
      67. DIR_NAME = dir_name + file["Path"]
      68. bp.upload(localpath= "." + file["Path"]+ "/" +file["fileName"], remotepath = str(DIR_NAME), ondup='newcopy')
      69. print ("文件發(fā)送完成:本地路徑:" + file["Path"]+"/" +file["fileName"] + " 遠(yuǎn)程文件夾:" + DIR_NAME)
      70. totalFileSize += get_FileSize( "." + file["Path"]+ "/" +file["fileName"])
      71. else:
      72. bp.upload(localpath= file["fileName"], remotepath= dir_name, ondup='newcopy')
      73. print ("文件發(fā)送完成:" + file["fileName"] + " 遠(yuǎn)程文件夾:" + dir_name)
      74. totalFileSize += get_FileSize( "." + file["Path"]+ "/" +file["fileName"])
      75. print ("------------------------------------")
      76. end = datetime.datetime.now() # 計(jì)時(shí)結(jié)束
      77. print("上傳文件總大小為" + str(totalFileSize) + "MB")
      78. print("花費(fèi)時(shí)間(s):" + str((end - start).seconds))
      79. print("\nupload ok")

       

      傳輸速度還是可以的!

       

      四、優(yōu)化

      1. 或許可以考慮一下多線程,或者多進(jìn)程發(fā)送,在文件較大較多的時(shí)候,傳輸時(shí)間會少挺多的。

      2. 可以統(tǒng)計(jì)文件的修改時(shí)間,按時(shí)間段來發(fā)送文件,達(dá)到局部更新的效果。

       

      五、最后

      emmm至于你說為啥放著客戶端不用,寫個(gè)鬼命令行,(⊙﹏⊙)好像也有道理哦,不過寫著玩唄。寫程序多好玩。

      thx for reading.

       

       

        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多