欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)分割文件及合并文件的方法

 更新時(shí)間:2015年07月10日 16:57:39   作者:Sephiroth  
這篇文章主要介紹了Python實(shí)現(xiàn)分割文件及合并文件的方法,涉及Python針對(duì)文件的分割與合并操作相關(guān)技巧,通過(guò)自定義函數(shù)split與join實(shí)現(xiàn)了文件的分割與合并操作,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)分割文件及合并文件的方法。分享給大家供大家參考。具體如下:

分割文件split.py如下:

#!/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

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Python中super的用法實(shí)例

    Python中super的用法實(shí)例

    這篇文章主要介紹了Python中super的用法實(shí)例,本文對(duì)比了普通繼承和super繼承的相關(guān)內(nèi)容,從運(yùn)行結(jié)果上看,普通繼承和super繼承是一樣的,但是其實(shí)它們的內(nèi)部運(yùn)行機(jī)制不一樣,這一點(diǎn)在多重繼承時(shí)體現(xiàn)得很明顯,需要的朋友可以參考下
    2015-05-05
  • Python list sort方法的具體使用

    Python list sort方法的具體使用

    list.sort()方法是Python的列表方法,用于對(duì)原列表進(jìn)行排序。本文詳細(xì)的介紹了list.sort的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-12-12
  • 深入淺析Python獲取對(duì)象信息的函數(shù)type()、isinstance()、dir()

    深入淺析Python獲取對(duì)象信息的函數(shù)type()、isinstance()、dir()

    這篇文章主要介紹了Python獲取對(duì)象信息的函數(shù)type()、isinstance()、dir()的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-09-09
  • python繪制字符畫(huà)視頻的示例代碼

    python繪制字符畫(huà)視頻的示例代碼

    網(wǎng)上有很多的字符畫(huà),看起來(lái)很炫酷,本文就通過(guò)一則示例實(shí)現(xiàn)字符畫(huà)視頻,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • python方法如何實(shí)現(xiàn)字符串反轉(zhuǎn)

    python方法如何實(shí)現(xiàn)字符串反轉(zhuǎn)

    這篇文章主要介紹了python方法如何實(shí)現(xiàn)字符串反轉(zhuǎn)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Python 使用ConfigParser操作ini配置文件

    Python 使用ConfigParser操作ini配置文件

    這篇文章主要介紹了Python 使用ConfigParser操作ini配置文件的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • python中re.findall函數(shù)實(shí)例用法

    python中re.findall函數(shù)實(shí)例用法

    在本篇文章里小編給大家整理了一篇關(guān)于python中re.findall函數(shù)實(shí)例用法相關(guān)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-09-09
  • Python面向?qū)ο笾腴T(mén)類(lèi)和對(duì)象

    Python面向?qū)ο笾腴T(mén)類(lèi)和對(duì)象

    這篇文章主要為大家介紹了Python入門(mén)類(lèi)和對(duì)象,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-12-12
  • python數(shù)字圖像處理之基本圖形的繪制

    python數(shù)字圖像處理之基本圖形的繪制

    這篇文章主要為大家介紹了python數(shù)字圖像處理之基本圖形的繪制,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Python實(shí)現(xiàn)批量文件分類(lèi)保存的示例代碼

    Python實(shí)現(xiàn)批量文件分類(lèi)保存的示例代碼

    當(dāng)我們電腦里面的文本或者或者文件夾太多了,有時(shí)候想找到自己想要的文件,只能通過(guò)去搜索文件名,這樣還是很麻煩的。本文將通過(guò)Python語(yǔ)言實(shí)現(xiàn)文件批量分類(lèi)保存,需要的可以參考一下
    2022-04-04

最新評(píng)論