python 实现百度网盘非会员上传超过500个文件的方法

作者:武散人 时间:2021-01-31 19:45:37 

案例故事:

百度网盘非会员大量上传文件,会弹出:“上传文件数量超出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个文件,后续将一个一个的文件夹拖入百度网盘(电脑客户端)即可了。

备注信息

  • 本脚本不涉及任何的删除文件或文件夹的操作,不会出现文件丢失情况。

  • 兼容非英文的文件夹或文件分割操作。

来源:https://www.zipython.com/#/detail?id=c6156f71199346f4a10ea5ccbe6aa0b6

标签:python,百度网盘,上传文件
0
投稿

猜你喜欢

  • 用ASP建立一个简单的聊天室

    2007-09-21 12:43:00
  • CSS应用的必要步骤:样式重设

    2008-06-11 13:29:00
  • python 对任意数据和曲线进行拟合并求出函数表达式的三种解决方案

    2021-10-02 12:40:16
  • 详解python使用pip安装第三方库(工具包)速度慢、超时、失败的解决方案

    2023-11-24 18:03:27
  • PHP封装的PDO数据库操作类实例

    2023-11-18 04:54:31
  • Python dict和defaultdict使用实例解析

    2022-12-24 20:44:19
  • Python实现绘制多种激活函数曲线详解

    2022-06-30 01:02:06
  • 对python csv模块配置分隔符和引用符详解

    2023-04-14 13:52:20
  • ASP 获取腾讯IP地址的代码

    2011-02-26 11:19:00
  • 如何从SQL数据库中调用图片?

    2009-11-15 19:59:00
  • ASP检测服务器相关的一些代码

    2008-01-25 19:20:00
  • python+selenium+PhantomJS抓取网页动态加载内容

    2021-01-28 15:41:24
  • python实现自动化脚本编写

    2023-11-13 14:58:14
  • python+logging+yaml实现日志分割

    2023-12-18 01:19:08
  • 网页图片按钮的生成与美化

    2008-12-12 13:03:00
  • Python如何查看并打印matplotlib中所有的colormap(cmap)类型

    2023-12-05 11:04:28
  • 在Python文件中指定Python解释器的方法

    2023-06-24 13:22:50
  • Python中生成Epoch的方法

    2021-06-27 15:21:15
  • asp入门之字符串函数介绍示例

    2008-11-04 20:18:00
  • 对于Python装饰器使用的一些建议

    2022-05-26 09:05:43
  • asp之家 网络编程 m.aspxhome.com