1. 程式人生 > 程式設計 >python 實現百度網盤非會員上傳超過500個檔案的方法

python 實現百度網盤非會員上傳超過500個檔案的方法

案例故事:

百度網盤非會員大量上傳檔案,會彈出:“上傳檔案數量超出500個現在,開通超級會員後可繼續上傳”,其實是限制拖入500張相片,並非限制上傳500張。

python 實現百度網盤非會員上傳超過500個檔案的方法

非會員如何將眾多檔案,分割成500一個的資料夾,不受拖入數量限制呢?

準備階段

  • os.walk()函式,可以樹形遍歷整個路徑下的資料夾列表和檔案列表
  • Path(路徑).parent屬性,可以獲取該“路徑”的父路徑
  • os.path.relpath("D:\aaa\bbb\ccc",start="D:\aaa")函式,可以返回“bbb\ccc”字串, 實現路徑裁剪。
  • os.sep 可以代表任何路徑分隔符
  • os.rename()函式,可以實現移動功能
  • sys.argv[1] 通過接收“待分割的路徑”引數的輸入

Python面向物件類形式

# python3.8
# coding=utf-8
 
import os
import sys
from pathlib import Path
 
 
class BaiduPanCutter(object):
  '''百度網盤500個檔案分割器'''
 
  def __init__(self,root_path,count=500):
    self.root_path = root_path
    self.count = count
    self.folder_file_dict = {} # 資料夾與其檔案列表的對映字典
    self.get_folders_files() # 獲取該根路徑下的所有資料夾列表和檔案列表
 
  def get_folders_files(self):
    '''獲取該根路徑下的所有資料夾列表和檔案列表'''
    for folders,_,files in os.walk(self.root_path):
      self.folder_file_dict[folders] = files
 
  def _split(self,arr,count):
    '''分割檔案列表,每500算一份'''
    arrs = []
    while len(arr) > count:
      piece = arr[:count]
      arrs.append(piece)
      arr = arr[count:]
    arrs.append(arr)
    return arrs
 
  # 分割檔案並放到新的檔案去
  def cut_file(self):
    '''分割並移動到新的資料夾'''
    for each_folder in self.folder_file_dict.keys():
      num = 1 # 以500為倍數,這是1倍
 
      # 將檔案路徑(摒棄當前路徑)轉成字串,用_隔開
      temp_path = os.path.relpath(each_folder,Path(self.root_path).parent)
      temp_path = temp_path.replace(os.sep,"_")
      print(temp_path)
 
      files_list = self.folder_file_dict[each_folder]
      file_group = self._split(files_list,self.count) # 按500來分割
 
      if len(file_group) > 1: # 有超過500個的檔案列表
        for each_group in file_group: # 遍歷每500份的檔案列表
          new_folder = os.path.join(self.root_path,temp_path + "_" + str(num)) # 新路徑
          if not os.path.exists(new_folder):
            os.mkdir(new_folder)
          for each_file in each_group:
            old_file = os.path.join(each_folder,each_file)
            new_file = os.path.join(new_folder,each_file)
            print("正在將%s 移動到 %s" % (old_file,new_file))
            os.rename(old_file,new_file)
          num = num + 1
      else: # 無超過500個的檔案列表
        new_folder = os.path.join(self.root_path,temp_path) # 新路徑
        if not os.path.exists(new_folder):
          os.mkdir(new_folder)
        for each_file in file_group[0]: #
          old_file = os.path.join(each_folder,each_file)
          new_file = os.path.join(new_folder,each_file)
          print("正在將%s 移動到 %s" % (old_file,new_file))
          os.rename(old_file,new_file)
 
 
if __name__ == '__main__':
  try:
    arg1 = sys.argv[1]
    if os.path.isdir(arg1):
      b_obj = BaiduPanCutter(arg1,500)
      b_obj.cut_file()
    else:
      print("非資料夾,執行方法:python %s 路徑資料夾" % sys.argv[0])
  except IndexError:
    print("未輸入待分割的路徑資料夾, 執行方法:python %s 路徑資料夾" % sys.argv[0])
  os.system("pause")

執行方式與效果

執行方式:將以上程式碼命名為:baidu_pan_500_cutter.py
通過命令:python baidu_pan_500_cutter.py D:\DCIM\Photos 執行

python 實現百度網盤非會員上傳超過500個檔案的方法

每個資料夾都不會超過500個檔案,後續將一個一個的資料夾拖入百度網盤(電腦客戶端)即可了。

備註資訊

  • 本指令碼不涉及任何的刪除檔案或資料夾的操作,不會出現檔案丟失情況。
  • 相容非英文的資料夾或檔案分割操作。

以上就是python 實現百度網盤非會員上傳超過500個檔案的詳細內容,更多關於python 百度網盤上傳超過500個檔案的資料請關注我們其它相關文章!