最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python分割文件的常用方法

 更新時間:2014年11月01日 10:13:06   投稿:shichen2014  
這篇文章主要介紹了python分割文件的常用方法,包括指定分割大小、按行分割與分割合并等技巧,非常實用,需要的朋友可以參考下

本文大家整理了一些比較好用的關(guān)于python分割文件的方法,方法非常的簡單實用。分享給大家供大家參考。具體如下:

例子1 指定分割文件大小

配置文件 config.ini:

復(fù)制代碼 代碼如下:
[global]
#原文件存放目錄
dir1=F:\work\python\3595\pyserver\test
#新文件存放目錄
dir2=F:\work\python\3595\pyserver\test1

python 代碼如下:

復(fù)制代碼 代碼如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os,sys,ConfigParser
class file_openate(object):
def __init__(self):
    #初如化讀取數(shù)據(jù)庫配置
    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="軟件有不確認性,前期使用最好先備份,以免發(fā)生數(shù)據(jù)丟失,確認備份后,請輸入要分割的字節(jié)大小,按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 "請輸入正確的數(shù)字,請重新輸入".decode('utf-8')
file_name=file_openate()
file_name.file_list()

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

復(fù)制代碼 代碼如下:
#!/usr/bin/env python
#--*-- coding:utf-8 --*--
import os
class SplitFiles():
    """按行分割文件"""
    def __init__(self, file_name, line_count=200):
        """初始化要分割的源文件名和分割后的文件行數(shù)"""
        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 : # 正常結(jié)束循環(huán)后將剩余的內(nèi)容寫入新文件中
                        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) : # 如果臨時目錄不存在則創(chuàng)建
            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):
        """將按行分割后的內(nèi)容寫入相應(yīng)的分割文件中"""
        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()

上面只是進行了分割了,如果我們又要合并怎么辦呢?下面這個例子可以實現(xiàn)分割與合并哦,大家一起看看。

例子3, 分割文件與合并函數(shù)

復(fù)制代碼 代碼如下:
#!/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
 

復(fù)制代碼 代碼如下:
#!/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程序設(shè)計有所幫助。

相關(guān)文章

  • Java文件與類動手動腦實例詳解

    Java文件與類動手動腦實例詳解

    在本篇文章里小編給大家整理的是關(guān)于Java文件與類動手動腦實例知識點,有需要的朋友們學(xué)習(xí)參考下。
    2019-11-11
  • Pycharm設(shè)置自動代碼提示的超詳細圖文教程

    Pycharm設(shè)置自動代碼提示的超詳細圖文教程

    有時候我們在使用pycharm編寫python代碼的時候,發(fā)現(xiàn)沒有代碼提示,怎么解決呢?下面這篇文章主要給大家介紹了關(guān)于Pycharm設(shè)置自動代碼提示的超詳細圖文教程,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2023-05-05
  • 對python pandas 畫移動平均線的方法詳解

    對python pandas 畫移動平均線的方法詳解

    今天小編就為大家分享一篇對python pandas 畫移動平均線的方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • Python中torch.norm()用法解析

    Python中torch.norm()用法解析

    本文主要介紹了Python中torch.norm()用法解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • 使用wxPython實現(xiàn)Windows11任務(wù)欄通知功能

    使用wxPython實現(xiàn)Windows11任務(wù)欄通知功能

    這篇文章主要為大家詳細介紹了如何使用 wxPython 模塊,在 Windows 11 中實現(xiàn)任務(wù)欄通知功能,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-10-10
  • Python計算回文數(shù)的方法

    Python計算回文數(shù)的方法

    這篇文章主要介紹了Python計算回文數(shù)的方法,實例分析了Python操作字符串的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • Python實現(xiàn)批量文件自定義命名

    Python實現(xiàn)批量文件自定義命名

    有時候我們經(jīng)常需要對某一個文件夾中的文件進行重命名修改,但是我們一個一個取修改將會非常繁瑣,下面我們就來利用Python實現(xiàn)批量文件自定義命名吧
    2024-11-11
  • 520使用Python實現(xiàn)“我愛你”表白

    520使用Python實現(xiàn)“我愛你”表白

    這篇文章主要介紹了520使用Python實現(xiàn)“我愛你”表白,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • python裝飾器與遞歸算法詳解

    python裝飾器與遞歸算法詳解

    本文給大家詳細講解了python中的裝飾器與遞歸算法,有需要的小伙伴可以來參考下,希望對大家學(xué)習(xí)Python能夠有所幫助
    2016-02-02
  • pandas 兩列時間相減換算為秒的方法

    pandas 兩列時間相減換算為秒的方法

    下面小編就為大家分享一篇pandas 兩列時間相減換算為秒的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04

最新評論

汕头市| 蛟河市| 福清市| 宕昌县| 汉川市| 柳林县| 崇仁县| 蛟河市| 漳浦县| 荣昌县| 呼图壁县| 阿城市| 诸城市| 赤水市| 乐至县| 丽江市| 荔波县| 札达县| 临颍县| 舞钢市| 遂昌县| 汪清县| 涿鹿县| 伊宁县| 金川县| 扎兰屯市| 兴和县| 塔城市| 淳安县| 涿鹿县| 梁山县| 汶上县| 聂拉木县| 莲花县| 新河县| 武功县| 新民市| 三原县| 四子王旗| 荔浦县| 交口县|