如何让代码查找文件大小以及如何让用户输入段大小

选择 | 换行 | 行号
  1. f = open("centaur_1.mpg","rb")
  2. #create a for loop and split file into 20 parts
  3. buffer=f.read()  # readinto a large buffer to check its size
  4. #f.close() #close the file
  5. filesize=len(buffer)
  6. print filesize
  7. splitsize=int(filesize/segmentsize)
  8. f.seek(0)  # reset to beginning of file
  9. #smallfbuf=[]
  10. for i in range(splitsize+1):
  11.     filename="part"+str(i)+".mpg"
  12.     f2= open(filename,"wb")
  13.     buffer2 = f.read(splitsize)
  14.     f2.write(buffer2)
  15.     f2.close()
  16. f.close()

从上面的代码中,我如何获得文件大小和用户输入的SegmentSize

# 回答1


看起来您可以导入os,然后使用getSize()函数查看文件大小:

选择 | 换行 | 行号
  1. import os
  2. size = os.path.getsize('centaur_1.mpg')
  3. print size
  4.  

所以我是在谷歌上搜索"pythonGet FileSize"得到的,我会在谷歌上搜索"pythonGet User Input",允许用户输入片段大小。
祝好运!

标签: python

添加新评论