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导出数据到Excel可读取的CSV文件的方法
May 12 Python
深入解析Python中的变量和赋值运算符
Oct 12 Python
利用Python脚本实现ping百度和google的方法
Jan 24 Python
Python正则表达式经典入门教程
May 22 Python
浅析Python 3 字符串中的 STR 和 Bytes 有什么区别
Oct 14 Python
详解django中url路由配置及渲染方式
Feb 25 Python
Scrapy-Redis结合POST请求获取数据的方法示例
May 07 Python
Python爬虫 bilibili视频弹幕提取过程详解
Jul 31 Python
python获取指定日期范围内的每一天,每个月,每季度的方法
Aug 08 Python
python自动化UI工具发送QQ消息的实例
Aug 27 Python
opencv-python 读取图像并转换颜色空间实例
Dec 09 Python
python实现删除列表中某个元素的3种方法
Jan 15 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
PHP 类相关函数的使用详解
2013/05/10 PHP
php二维码生成
2015/10/19 PHP
php使用正则表达式获取字符串中的URL
2016/12/29 PHP
PHP读取、解析eml文件及生成网页的方法示例
2017/09/04 PHP
PHP数组array类常见操作示例
2020/05/15 PHP
WebGame《逆转裁判》完整版 代码下载(1月24日更新)
2007/01/29 Javascript
JS幻灯片可循环播放可平滑旋转带滚动导航(自写)
2013/08/05 Javascript
jquery中加载图片自适应大小主要实现代码
2013/08/23 Javascript
js函数定时器实现定时读取系统实时连接数
2014/04/30 Javascript
JavaScript递归操作实例浅析
2016/10/31 Javascript
JavaScript+Html5实现按钮复制文字到剪切板功能(手机网页兼容)
2017/03/30 Javascript
jquery.form.js异步提交表单详解
2017/04/25 jQuery
react-router中的属性详解
2017/06/01 Javascript
webpack开发跨域问题解决办法
2017/08/03 Javascript
JavaScript实现省市联动过程中bug的解决方法
2017/12/04 Javascript
编写React组件项目实践分析
2018/03/04 Javascript
javascript和php使用ajax通信传递JSON的实例
2018/08/21 Javascript
vue.js 图片上传并预览及图片更换功能的实现代码
2018/08/27 Javascript
JavaScript数据结构与算法之二叉树插入节点、生成二叉树示例
2019/02/21 Javascript
javascript实现对话框功能警告(alert 消息对话框)确认(confirm 消息对话框)
2019/05/07 Javascript
Vue基本指令实例图文讲解
2021/02/25 Vue.js
[55:45]DOTA2上海特级锦标赛D组败者赛 Liquid VS COL第一局
2016/02/28 DOTA
python的三目运算符和not in运算符使用示例
2014/03/03 Python
Python中使用tarfile压缩、解压tar归档文件示例
2015/04/05 Python
Python实现PS图像调整黑白效果示例
2018/01/25 Python
Python处理文本换行符实例代码
2018/02/03 Python
浅谈Python xlwings 读取Excel文件的正确姿势
2021/02/26 Python
Pytorch之扩充tensor的操作
2021/03/04 Python
世界上最大的罕见唱片、CD和音乐纪念品网上商店:991.com
2018/05/03 全球购物
乐高奥地利官方商店:LEGO Shop AT
2019/07/16 全球购物
给面试官的感谢信
2014/02/01 职场文书
2015年银行工作总结范文
2015/04/01 职场文书
2015年社区计生工作总结
2015/04/21 职场文书
幼儿园安全管理制度
2015/08/05 职场文书
MySQL数据库中的锁、解锁以及删除事务
2022/05/06 MySQL
JavaScript实现九宫格拖拽效果
2022/06/28 Javascript