百度云盘上传时限制目录下的文件数目不能超过500,对有几千张照片的目录,手工分隔文件很麻烦,于是写了一个python小程序,将文件切分为480个文件一个的目录:
folder_process_for_baidu_upload.py
-----------------------
# coding=gbk
import os
import os.path
import shutil
import time
import struct
import re
# max baidu upload dir file number limit to 500, here we take 480 to split
MAX_BAIDU_ONCE_UPLOAD_FILENUM = 480
# 指明被遍历的文件夹
#targetDir = "D:\\Temp\\2016.08.22_导出x2_lp_yp"
targetDir = input("Input Target Dir: ")
#print targetDir
targetDirName = os.path.basename(targetDir)
targetParentDir = os.path.dirname(targetDir)
'''
#------------------------------------------------
#显示给定目录的文件列表
print "targetDir=" + targetDir
for parent,dirnames,fileNames in os.walk(targetDir): #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字
for dirname in dirnames: #输出文件夹信息
print "dirname: " + dirname
for fileName in fileNames: #输出文件信息
print "fileName: " + fileName
#print "the full name of the file is:" + os.path.join(parent,fileName) #输出文件路径信息
#------------------------------------------------
'''
'''
查看文件夹下的所有文件及文件夹, join为拼接函数
'''
#------------------------------------------------
def ShowDirFileList(path):
for parentDir, dirs, files in os.walk(path, True):
print parentDir #主目录
for item in files: #主目录下的文件夹
print os.path.join(parentDir, item)
#------------------------------------------------
'''
计算文件夹大小
'''
#------------------------------------------------
def CalDirSize(path):
size = 0L
for parentDir, dirs, files in os.walk(path, True):
#print "parentDir=" + parentDir
size += sum([os.path.getsize(os.path.join(parentDir, name)) for name in files])
return size
#------------------------------------------------
'''
统计文件夹中文件数目
'''
#------------------------------------------------
def StatFileCount(path):
targetDirFileCount = 0L
for parentDir, dirs, files in os.walk(path, True):
targetDirFileCount += len(files)
return targetDirFileCount
#------------------------------------------------
'''
目录/文件大小格式化显示
'''
#------------------------------------------------
def ShowFormatSize(size):
if (size < 1024):
summary = bytes(size) + ' B'
elif ((size/1024.0) < 1024):
summary = '%.1f'% (size/1024.0/1024.0) + ' KB'
elif ((size/1024.0/1024.0) < 1024):
summary = '%.1f'% (size/1024.0/1024.0) + ' MB'
else:
summary = '%.1f'% (size/1024.0/1024.0/1024.0) + ' GB'
return summary + " (" + "{:,}".format(size) + " 字节)"
#------------------------------------------------
if __name__ == '__main__':
#ShowDirFileList(targetDir)
#dirSize = CalDirSize(targetDir)
targetDirFileCount = StatFileCount(targetDir)
print targetDir + ", file num " + bytes(targetDirFileCount)
forkCount = 0
while (targetDirFileCount > MAX_BAIDU_ONCE_UPLOAD_FILENUM):
forkCount += 1
forkDir = os.path.join(targetParentDir, targetDirName + "_" + bytes(forkCount))
print "#" + bytes(forkCount) + ": targetDirFileCount=" + bytes(targetDirFileCount) + ", Fork New Dir: " + forkDir
if not os.path.exists(forkDir):
os.mkdir(forkDir)
#traverse all the files and move to new fork dir, reduce target dir size one by one
forkDirFileCount = 0
for rootDir, dirs, files in os.walk(targetDir, True):
print "walking on " + rootDir
for item in files:
#move the file to new forked dir
itemFile = os.path.join(rootDir, item)
itemNew = os.path.join(forkDir, item)
shutil.move(itemFile, itemNew)
targetDirFileCount -= 1
forkDirFileCount += 1
#print itemFile + " -> " + itemNew + ", count=" + bytes(forkDirFileCount)
if (forkDirFileCount >= MAX_BAIDU_ONCE_UPLOAD_FILENUM or targetDirFileCount <= MAX_BAIDU_ONCE_UPLOAD_FILENUM):
break
if (forkDirFileCount >= MAX_BAIDU_ONCE_UPLOAD_FILENUM or targetDirFileCount <= MAX_BAIDU_ONCE_UPLOAD_FILENUM):
break