- 论坛徽章:
- 0
|
最近在学习python,写了一个生成文件的脚本但在生成大文件时发现效率比较低,生成一个5G文件时等待时间会比较长。
以下是自己写的生成文件脚本,希望高手能优化或是给出其它的好的办法。。。有问题的地方也请高手指出。。。
#coding=utf-8
'''
Created on 2012-5-29
@author: xiaochou
'''
import os
import time
#获取文件的属性值
def getfile_att(filepath,*att_type):
file_stat=os.stat(filepath)
file_st={}
if 'fctime' in att_type:
file_st['fctime']=str(int(file_stat.st_ctime))
if 'fmtime' in att_type:
file_st['fmtime']=str(int(file_stat.st_mtime))
if 'fatime' in att_type:
file_st['fatime']=str(int(file_stat.st_atime))
if 'fmode' in att_type:
file_st['fmode']=str(file_stat.st_mode)
if 'fsize' in att_type:
file_st['fsize']=str(int(file_stat.st_size))
return file_st
def file_size(size):
local_time = time.strftime("%Y%m%d%H%M%S",time.localtime())
file_name = "E:\\testFile\\"+str(local_time)+".txt"
f = open(file_name,'ab')
note = "测试文件内容"
for i in range(1,99999999):
f.write(note+str(i))
filesize = float(getfile_att(file_name,'fsize')["fsize"])
if filesize/1024/1024 >= size:
break
else:
continue
f.close()
print "ALL down!"
if __name__ == '__main__':
size = input("input you's size:")
file_size(size)
print file_size |
|