Tôi biết điều này muộn (một chút), nhưng đối với bất kỳ ai khác tình cờ gặp câu hỏi này và có cùng vấn đề ... Vấn đề của tôi đặc biệt là tôi có hàng trăm hợp đồng biểu diễn các tệp không còn cần thiết nữa các máy chủ Dropbox và tôi không muốn làm sạch một HDD chỉ để có thể xóa chúng với đồng bộ chọn lọc.
Xóa nhiều tệp vẫn không thể thực hiện được trên giao diện web, nhưng nếu bạn không ngại lặn vào Dropbox API, điều này ít nhất có thể được tự động và bạn sẽ không phải sử dụng bộ nhớ của riêng mình bên dưới với số Python SDK, nhưng có các tùy chọn ngôn ngữ khác). Giới hạn tệp vẫn áp dụng, nhưng số lượng tệp trong mỗi thư mục có thể được tính để xác định đúng cách để xóa chúng mà không gặp sự cố. Giống như vậy:
Tập lệnh sau sẽ lấy khóa API Dropbox duy nhất của bạn và danh sách các thư mục Dropbox (deleteDirList
) làm đầu vào. Sau đó nó lặp qua từng thư mục con của mỗi phần tử của deleteDirList
để xác định xem có đủ tệp để có thể xóa thư mục mà không đạt đến giới hạn hay không (tôi đặt giới hạn thành tệp bảo thủ (?) 10.000; nếu có quá nhiều tệp, nó sẽ xóa các tệp riêng lẻ cho đến khi số lượng vượt quá giới hạn. Bạn sẽ cần phải cài đặt gói Python dropbox
(Tôi sử dụng Anaconda, vì vậy conda install dropbox
)
Ghi nhớ đây là cách tiếp cận vũ phu; mỗi thư mục con sẽ bị xóa từng cái một có thể mất nhiều thời gian. Phương pháp tốt hơn là đếm các tệp trong từng thư mục con, sau đó xác định thư mục cấp cao nhất có thể bị xóa mà không đạt đến giới hạn, nhưng tiếc là tôi không có thời gian để triển khai tại thời điểm này.
import dropbox
##### USER INPUT #####
appToken = r'DROPBOX APIv2 TOKEN'
deleteDirList = ['/directoryToDelete1/','/directoryToDelete2/'] # list of Dropbox paths in UNIX format (where Dropbox root is specified as '/') within which all contents will be deleted. The script will go through these one at a time. These need to be separate directories; subdirectories will deleted in the loop and will throw an exception if listed here.
######################
dbx = dropbox.Dropbox(appToken)
modifyLimit = 10000
# Loop through each path in deleteDirList
for deleteStartDir in deleteDirList:
deleteStartDir = deleteStartDir.lower()
# Initialize pathList. This is the variable that records all directories down each path tree so that each directory is traversed, files counted, then deleted
pathList = [deleteStartDir]
# Deletion loop
try:
while 1:
# Determine if there is a subdirectory in the current directory. If not, set nextDir=False
nextDir = next((x.path_lower for x in dbx.files_list_folder(pathList[-1]).entries if isinstance(x,dropbox.files.FolderMetadata)),False)
if not not nextDir: # if nextDir has a value, append the subdirectory to pathList
pathList.append(nextDir)
else: # otherwise, delete the current directory
if len(pathList)<=1: # if this is the root deletion directory (specified in deleteDirList), delete all files and keep folder
fileList = [x.path_lower for x in dbx.files_list_folder(pathList[-1]).entries]
print('Cannot delete start directory; removing final',len(fileList),'file(s)')
for filepath in fileList:
dbx.files_delete(filepath)
raise EOFError() # deletion script is complete
# Count the number of files. If fileCnt>=modifyLimit, remove files until fileCnt<modifyLimit, then delete the remainder of the directory
fileCnt = len(dbx.files_list_folder(pathList[-1]).entries)
if fileCnt<modifyLimit:
print('Deleting "{path}" and'.format(path=pathList[-1]),fileCnt,'file(s) within\n')
else:
print('Too many files to delete directory. Deleting',fileCnt-(modifyLimit+1),'file(s) to reduce count, then removing',pathList[-1],'\n')
fileList = [x.path_lower for x in dbx.files_list_folder(pathList[-1]).entries]
for filepath in fileList[-modifyLimit:]:
dbx.files_delete(filepath)
dbx.files_delete(pathList[-1])
del pathList[-1]
except EOFError:
print('Deleted all relevant files and directories from "{}"'.format(deleteStartDir))
Tổng kích thước của 30.000 tệp là bao nhiêu? – Flukey
@Flukey: có thể 500mb đến 1GB. Chúng khá nhỏ. – Zach
Tôi đã giải quyết vấn đề này bằng cách hút nó lên và đợi 12 giờ để các tệp tải xuống tất cả các máy khác. Sau đó, tôi đã xóa chúng, và mọi thứ bây giờ là màu đào. – Zach