python分割文件的常用方法


Posted in Python onNovember 01, 2014

本文大家整理了一些比较好用的关于python分割文件的方法,方法非常的简单实用。分享给大家供大家参考。具体如下:

例子1 指定分割文件大小

配置文件 config.ini:

[global]

#原文件存放目录

dir1=F:\work\python\3595\pyserver\test

#新文件存放目录

dir2=F:\work\python\3595\pyserver\test1

python 代码如下:

#!/usr/bin/python

# -*- coding: utf-8 -*-

import os,sys,ConfigParser

class file_openate(object):

def __init__(self):

    #初如化读取数据库配置

    dir_config = ConfigParser.ConfigParser()

    file_config=open('config.ini',"rb")

    dir_config.readfp(file_config)

    self.dir1=str(dir_config.get("global","dir1"))

    self.dir1=unicode(self.dir1,'utf8')

    self.dir2=str(dir_config.get("global","dir2"))

    self.dir2=unicode(self.dir2,'utf8')

    file_config.close()

#print self.dir2

#self.dir1="F:\\work\\python\\3595\\pyserver\\test"

def file_list(self):

    input_name_han="软件有不确认性,前期使用最好先备份,以免发生数据丢失,确认备份后,请输入要分割的字节大小,按b来计算".decode('utf-8')

    print input_name_han

    while 1:

input_name=raw_input("number:")

if input_name.isdigit():

    input_name=int(input_name)

    os.chdir(self.dir1)

    for filename in os.listdir(self.dir1):

os.chdir(self.dir1)

#print filename

name, ext = os.path.splitext(filename)

file_size=int(os.path.getsize(filename))

f=open(filename,'r')

chu_nmuber=0

while file_size >= 1:

    #print file_size

    chu_nmuber=chu_nmuber + 1

    if file_size >= input_name:

file_size=file_size - input_name

a=f.read(input_name)

os.chdir(self.dir2)

filename1=name + '-' + str(chu_nmuber) + ext

new_f=open(filename1,'a')

new_f.write(a)

new_f.close()

#print file_size

    else:

a=f.read()

os.chdir(self.dir2)

filename1=name + '-' + str(chu_nmuber) + ext

new_f=open(filename1,'a')

new_f.write(a)

new_f.close()

break

print "分割成功".decode('utf-8') + filename

f.close()

else:

    print "请输入正确的数字,请重新输入".decode('utf-8')

file_name=file_openate()

file_name.file_list()

例子2,按行分割文件大小

#!/usr/bin/env python

#--*-- coding:utf-8 --*--

import os

class SplitFiles():

    """按行分割文件"""

    def __init__(self, file_name, line_count=200):

        """初始化要分割的源文件名和分割后的文件行数"""

        self.file_name = file_name

        self.line_count = line_count

    def split_file(self):

        if self.file_name and os.path.exists(self.file_name):

            try:

                with open(self.file_name) as f : # 使用with读文件

                    temp_count = 0

                    temp_content = []

                    part_num = 1

                    for line in f:

                        if temp_count < self.line_count:

                            temp_count += 1

                        else :

                            self.write_file(part_num, temp_content)

                            part_num += 1

                            temp_count = 1

                            temp_content = []

                        temp_content.append(line)

                    else : # 正常结束循环后将剩余的内容写入新文件中

                        self.write_file(part_num, temp_content)

            except IOError as err:

                print(err)

        else:

            print("%s is not a validate file" % self.file_name)

    def get_part_file_name(self, part_num):

        """"获取分割后的文件名称:在源文件相同目录下建立临时文件夹temp_part_file,然后将分割后的文件放到该路径下"""

        temp_path = os.path.dirname(self.file_name) # 获取文件的路径(不含文件名)

        part_file_name = temp_path + "temp_part_file"

        if not os.path.exists(temp_path) : # 如果临时目录不存在则创建

            os.makedirs(temp_path)

        part_file_name += os.sep + "temp_file_" + str(part_num) + ".part"

        return part_file_name

    def write_file(self, part_num, *line_content):

        """将按行分割后的内容写入相应的分割文件中"""

        part_file_name = self.get_part_file_name(part_num)

        print(line_content)

        try :

            with open(part_file_name, "w") as part_file:

                part_file.writelines(line_content[0])

        except IOError as err:

            print(err)

if __name__ == "__main__":

    sf = SplitFiles(r"F:\multiple_thread_read_file.txt")

    sf.split_file()

上面只是进行了分割了,如果我们又要合并怎么办呢?下面这个例子可以实现分割与合并哦,大家一起看看。

例子3, 分割文件与合并函数

#!/usr/bin/python

##########################################################################

# split a file into a set of parts; join.py puts them back together;

# this is a customizable version of the standard unix split command-line 

# utility; because it is written in Python, it also works on Windows and

# can be easily modified; because it exports a function, its logic can 

# also be imported and reused in other applications;

##########################################################################

      

import sys, os

kilobytes = 1024

megabytes = kilobytes * 1000

chunksize = int(1.4 * megabytes)   # default: roughly a floppy

      

def split(fromfile, todir, chunksize=chunksize): 

    if not os.path.exists(todir):  # caller handles errors

os.mkdir(todir)    # make dir, read/write parts

    else:

for fname in os.listdir(todir):    # delete any existing files

    os.remove(os.path.join(todir, fname)) 

    partnum = 0

    input = open(fromfile, 'rb')   # use binary mode on Windows

    while 1:       # eof=empty string from read

chunk = input.read(chunksize)      # get next part <= chunksize

if not chunk: break

partnum  = partnum+1

filename = os.path.join(todir, ('part%04d' % partnum))

fileobj  = open(filename, 'wb')

fileobj.write(chunk)

fileobj.close()    # or simply open().write()

    input.close()

    assert partnum <= 9999 # join sort fails if 5 digits

    return partnum

     

if __name__ == '__main__':

    if len(sys.argv) == 2 and sys.argv[1] == '-help':

print 'Use: split.py [file-to-split target-dir [chunksize]]'

    else:

if len(sys.argv) < 3:

    interactive = 1

    fromfile = raw_input('File to be split? ')       # input if clicked 

    todir    = raw_input('Directory to store part files? ')

else:

    interactive = 0

    fromfile, todir = sys.argv[1:3]  # args in cmdline

    if len(sys.argv) == 4: chunksize = int(sys.argv[3])

absfrom, absto = map(os.path.abspath, [fromfile, todir])

print 'Splitting', absfrom, 'to', absto, 'by', chunksize

      

try:

    parts = split(fromfile, todir, chunksize)

except:

    print 'Error during split:'

    print sys.exc_info()[0], sys.exc_info()[1]

else:

    print 'Split finished:', parts, 'parts are in', absto

if interactive: raw_input('Press Enter key') # pause if clicked

join_file.py
 

#!/usr/bin/python

##########################################################################

# join all part files in a dir created by split.py, to recreate file.  

# This is roughly like a 'cat fromdir/* > tofile' command on unix, but is 

# more portable and configurable, and exports the join operation as a 

# reusable function.  Relies on sort order of file names: must be same 

# length.  Could extend split/join to popup Tkinter file selectors.

##########################################################################

      

import os, sys

readsize = 1024

      

def join(fromdir, tofile):

    output = open(tofile, 'wb')

    parts  = os.listdir(fromdir)

    parts.sort()

    for filename in parts:

filepath = os.path.join(fromdir, filename)

fileobj  = open(filepath, 'rb')

while 1:

    filebytes = fileobj.read(readsize)

    if not filebytes: break

    output.write(filebytes)

fileobj.close()

    output.close()

      

if __name__ == '__main__':

    if len(sys.argv) == 2 and sys.argv[1] == '-help':

print 'Use: join.py [from-dir-name to-file-name]'

    else:

if len(sys.argv) != 3:

    interactive = 1

    fromdir = raw_input('Directory containing part files? ')

    tofile  = raw_input('Name of file to be recreated? ')

else:

    interactive = 0

    fromdir, tofile = sys.argv[1:]

absfrom, absto = map(os.path.abspath, [fromdir, tofile])

print 'Joining', absfrom, 'to make', absto

      

try:

    join(fromdir, tofile)

except:

    print 'Error joining files:'

    print sys.exc_info()[0], sys.exc_info()[1]

else:

   print 'Join complete: see', absto

if interactive: raw_input('Press Enter key') # pause if clicked

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python中pygame的mouse鼠标事件用法实例
Nov 11 Python
Python 中开发pattern的string模板(template) 实例详解
Apr 01 Python
python实现发送邮件功能代码
Dec 14 Python
Python序列循环移位的3种方法推荐
Apr 09 Python
更新修改后的Python模块方法
Mar 03 Python
OpenCV3.0+Python3.6实现特定颜色的物体追踪
Jul 23 Python
python图片二值化提高识别率代码实例
Aug 24 Python
对Tensorflow中Device实例的生成和管理详解
Feb 04 Python
python获取依赖包和安装依赖包教程
Feb 13 Python
将数据集制作成VOC数据集格式的实例
Feb 17 Python
使用python实现名片管理系统
Jun 18 Python
Python基于network模块制作电影人物关系图
Jun 19 Python
跟老齐学Python之通过Python连接数据库
Oct 28 #Python
Python对象体系深入分析
Oct 28 #Python
Python中类的继承代码实例
Oct 28 #Python
Python列表list数组array用法实例解析
Oct 28 #Python
python实现无证书加密解密实例
Oct 27 #Python
深入理解Python 代码优化详解
Oct 27 #Python
简单的Python抓taobao图片爬虫
Oct 26 #Python
You might like
table标签的结构与合并单元格的实现方法
2013/07/24 PHP
PHP关联数组实现根据元素值删除元素的方法
2015/06/26 PHP
PHP中set error handler函数用法小结
2015/11/11 PHP
PHP+Ajax实现的检测用户名功能简单示例
2019/02/12 PHP
Yii Framework框架开发微信公众平台示例
2020/04/26 PHP
鼠标图片振动代码
2006/07/06 Javascript
检测jQuery.js是否已加载的判断代码
2011/05/20 Javascript
js与jquery中获取当前鼠标的x、y坐标位置的代码
2011/05/23 Javascript
Eclipse下jQuery文件报错出现错误提示红叉
2014/01/13 Javascript
js获取UserControl内容为拼html时提供方便
2014/11/02 Javascript
浅谈JS使用[ ]来访问对象属性
2016/09/21 Javascript
jQuery EasyUI Layout实现tabs标签的实例
2017/09/26 jQuery
使用ECharts实现状态区间图
2018/10/25 Javascript
微信小程序使用车牌号输入法的示例代码
2019/08/20 Javascript
微信小程序实现星级评价
2019/11/20 Javascript
Python urllib模块urlopen()与urlretrieve()详解
2013/11/01 Python
Django中实现一个高性能计数器(Counter)实例
2014/07/09 Python
以911新闻为例演示Python实现数据可视化的教程
2015/04/23 Python
python数据类型_字符串常用操作(详解)
2017/05/30 Python
python利用微信公众号实现报警功能
2018/06/10 Python
使用Python爬虫库requests发送表单数据和JSON数据
2020/01/25 Python
SpringBoot首页设置解析(推荐)
2021/02/11 Python
英国最大的网上药品商店:Chemist Direct
2017/12/16 全球购物
介绍一下Linux文件的记录形式
2012/04/18 面试题
感恩之星事迹材料
2014/05/03 职场文书
大学生求职信范文
2014/05/24 职场文书
团队拓展活动方案
2014/08/28 职场文书
教师工作总结范文2014
2014/11/10 职场文书
物业前台接待岗位职责
2015/04/03 职场文书
学历证明样本
2015/06/16 职场文书
遗失证明范文
2015/06/19 职场文书
老乡会致辞
2015/07/28 职场文书
《伯牙绝弦》教学反思
2016/02/16 职场文书
opencv-python图像配准(匹配和叠加)的实现
2021/06/23 Python
Python Django项目和应用的创建详解
2021/11/27 Python
人物搭配车车超萌联名预备中 【咒术迴战】 ⨯ 【天竺鼠车车】 展开合作
2022/04/11 日漫