Home > Backend Development > Python Tutorial > python简单分割文件的方法

python简单分割文件的方法

WBOY
Release: 2016-06-06 11:14:45
Original
1365 people have browsed it

本文实例讲述了python简单分割文件的方法。分享给大家供大家参考。具体如下:

有的网站在上传文件时对文件大小有限制,因此可以将大文件分割成多个小文件再上传。

#!/usr/bin/env python
def split(filename, size):
  fp = open(filename, 'rb')
  i = 0
  n = 0
  temp = open(filename+'.part'+str(i),'wb')
  buf = fp.read(1024)
  while(True):
    temp.write(buf)
    buf = fp.read(1024)
    if(buf == ''):
      print filename+'.part'+str(i)+';'
      temp.close()
      fp.close()
      return
    n += 1
    if(n == size):
      n = 0
      print filename+'.part'+str(i)+';'
      i += 1
      temp.close()
      temp = open(filename+'.part'+str(i),'wb')
if __name__ == '__main__':
  name = raw_input('input filename:')
  split(name, 307200) #分割后每个文件300M
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template