python 實現(xiàn)百度網(wǎng)盤非會員上傳超過500個文件的方法
案例故事:
百度網(wǎng)盤非會員大量上傳文件,會彈出:“上傳文件數(shù)量超出500個現(xiàn)在,開通超級會員后可繼續(xù)上傳”,其實是限制拖入500張相片,并非限制上傳500張。
非會員如何將眾多文件,分割成500一個的文件夾,不受拖入數(shù)量限制呢?
準(zhǔn)備階段
- os.walk()函數(shù),可以樹形遍歷整個路徑下的文件夾列表和文件列表
- Path(路徑).parent屬性,可以獲取該“路徑”的父路徑
- os.path.relpath("D:\aaa\bbb\ccc",start="D:\aaa")函數(shù),可以返回“bbb\ccc”字符串, 實現(xiàn)路徑裁剪。
- os.sep 可以代表任何路徑分隔符
- os.rename()函數(shù),可以實現(xiàn)移動功能
- sys.argv[1] 通過接收“待分割的路徑”參數(shù)的輸入
Python面向?qū)ο箢愋问?/h2>
# python3.8
# coding=utf-8
import os
import sys
from pathlib import Path
class BaiduPanCutter(object):
'''百度網(wǎng)盤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為倍數(shù),這是1倍
# 將文件路徑(摒棄當(dāng)前路徑)轉(zhuǎn)成字符串,用_隔開
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")
運行方式與效果
# python3.8 # coding=utf-8 import os import sys from pathlib import Path class BaiduPanCutter(object): '''百度網(wǎng)盤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為倍數(shù),這是1倍 # 將文件路徑(摒棄當(dāng)前路徑)轉(zhuǎn)成字符串,用_隔開 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 運行
每個文件夾都不會超過500個文件,后續(xù)將一個一個的文件夾拖入百度網(wǎng)盤(電腦客戶端)即可了。
備注信息
- 本腳本不涉及任何的刪除文件或文件夾的操作,不會出現(xiàn)文件丟失情況。
- 兼容非英文的文件夾或文件分割操作。
以上就是python 實現(xiàn)百度網(wǎng)盤非會員上傳超過500個文件的詳細(xì)內(nèi)容,更多關(guān)于python 百度網(wǎng)盤上傳超過500個文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在python3中pyqt5和mayavi不兼容問題的解決方法
今天小編就為大家分享一篇在python3中pyqt5和mayavi不兼容問題的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01python實現(xiàn)簡單的飛機(jī)大戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)簡單的飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05Pandas數(shù)據(jù)合并的兩種實現(xiàn)方法
本文主要介紹了Pandas數(shù)據(jù)合并的兩種實現(xiàn)方法,DataFrame數(shù)據(jù)合并主要使用merge()方法和concat()方法,具有一定的參考價值,感興趣的可以了解一下2023-11-11Python使用plt.boxplot() 參數(shù)繪制箱線圖
這篇文章主要介紹了Python使用plt.boxplot() 參數(shù)繪制箱線圖 ,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06Python DataFrame實現(xiàn)固定周期內(nèi)統(tǒng)計每列的非零值
在數(shù)據(jù)處理中,使用DataFrame統(tǒng)計固定周期內(nèi)每列的非零值數(shù)量是一種常見需求,通過將數(shù)據(jù)分組并使用計數(shù)函數(shù),可以方便地實現(xiàn)此目標(biāo),具體方法包括首先計算每列的0值個數(shù),然后通過總數(shù)減去0值個數(shù)得到非零值的數(shù)量2024-09-09淺談JupyterNotebook導(dǎo)出pdf解決中文的問題
這篇文章主要介紹了淺談JupyterNotebook導(dǎo)出pdf解決中文的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python實現(xiàn)的文軒網(wǎng)爬蟲完整示例
這篇文章主要介紹了Python實現(xiàn)的文軒網(wǎng)爬蟲,結(jié)合完整實例形式分析了Python爬蟲爬取文軒網(wǎng)圖書信息的相關(guān)操作技巧,需要的朋友可以參考下2019-05-05