python切分目录
来源:互联网 发布:mac os 10.7 升级 编辑:程序博客网 时间:2024/06/05 02:26
百度云盘上传时限制目录下的文件数目不能超过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
- python切分目录
- python切分目录
- python读取并切分
- Python切分字符串
- 使用PYTHON切分、合并文件
- 【python】按行切分文本文件
- python切分apache日志文件
- [LintCode 107] 单词切分(Python)
- python 和 java 的字符串切分问题
- 6.python学习笔记:切分大型文件
- Python切分字符串的一个不错方法
- python目录
- python 3.0 遍历目录
- python文件目录操作
- python中文目录
- (Python编程)目录工具
- Python文件、目录函数
- 《Python核心编程》目录
- 通讯录设计(链表实现)
- spring集成slf4j日志输出
- js封装方法
- 自定义折线图/柱状图
- 赋值运算符的重载函数
- python切分目录
- 设计模式之单例设计模式(饿汉单例设计模式&懒汉单例设计模式)
- iOS开发:给项目工程里面添加Podfile配置文件,以及CocoaPods的简单使用
- Java Object.hashCode()方法
- 51nod 1460 连接小岛
- 学习matlab GUI编程的一点点所得
- C++编程->基于CSerialPort类的串口通信测试
- hibernate QBC查询
- NYOJ Registration system