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

Python實現分割文件及合并文件的方法

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

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

分割文件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

希望本文所述對大家的Python程序設計有所幫助。

相關文章

  • python中opencv支持向量機的實現

    python中opencv支持向量機的實現

    本文主要介紹了python中opencv支持向量機的實現,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 用python處理圖片之打開\顯示\保存圖像的方法

    用python處理圖片之打開\顯示\保存圖像的方法

    本篇文章主要介紹了用python處理圖片之打開\顯示\保存圖像的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Python中itertools簡介使用介紹

    Python中itertools簡介使用介紹

    itertools是python內置的模塊,使用簡單且功能強大,itertools模塊標準化了一個快速、高效利用內存的核心工具集,這些工具本身或組合都很有用,這篇文章主要介紹了Python中itertools詳解,需要的朋友可以參考下
    2022-12-12
  • Scrapy爬蟲實例講解_?;ňW

    Scrapy爬蟲實例講解_?;ňW

    下面小編就為大家?guī)硪黄猄crapy爬蟲實例講解_校花網。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • 跟老齊學Python之折騰一下目錄

    跟老齊學Python之折騰一下目錄

    本講只關注os.path,真所謂“弱水三千,只取一瓢”,為什么這么偏愛它呢?因為它和前面已經講過的文件操作進行配合,就能夠隨心所欲操作各個地方的文件了
    2014-10-10
  • python中redis查看剩余過期時間及用正則通配符批量刪除key的方法

    python中redis查看剩余過期時間及用正則通配符批量刪除key的方法

    這篇文章主要介紹了python中redis查看剩余過期時間及用正則通配符批量刪除key的方法,需要的朋友可以參考下
    2018-07-07
  • python實現根據指定字符截取對應的行的內容方法

    python實現根據指定字符截取對應的行的內容方法

    今天小編就為大家分享一篇python實現根據指定字符截取對應的行的內容方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • Python利用Diagrams繪制漂亮的系統架構圖

    Python利用Diagrams繪制漂亮的系統架構圖

    Diagrams  是一個基于Python繪制云系統架構的模塊,它能夠通過非常簡單的描述就能可視化架構。本文將利用它繪制漂亮的系統架構圖,感興趣的可以了解一下
    2023-01-01
  • python神經網絡Keras實現LSTM及其參數量詳解

    python神經網絡Keras實現LSTM及其參數量詳解

    這篇文章主要為大家介紹了python神經網絡Keras實現LSTM及其參數量詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • Python3.8對可迭代解包的改進及用法詳解

    Python3.8對可迭代解包的改進及用法詳解

    這篇文章主要介紹了Python3.8對可迭代解包的改進及用法詳解,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10

最新評論

宜城市| 工布江达县| 上栗县| 乌兰浩特市| 灵川县| 乌鲁木齐县| 射洪县| 平果县| 庆云县| 阳曲县| 子长县| 盐边县| 奉化市| 黄大仙区| 靖西县| 通辽市| 淮南市| 赤城县| 盖州市| 威信县| 登封市| 吉木萨尔县| 西乌珠穆沁旗| 冷水江市| 黎川县| 会理县| 边坝县| 牙克石市| 宿松县| 安徽省| 黑水县| 伽师县| 方山县| 岢岚县| 中西区| 乡城县| 全州县| 政和县| 长沙市| 通江县| 巴塘县|