原文数据:
无
具体要求:
1. 模拟Linux的dd命令快速创建大文件
代码实现:
# -*- coding: utf-8 -*-
"""
#
# Authors: limanman
# OsChina: http://my.oschina.net/pydevops/
# Purpose:
#
"""
import sys
import os
def create_bigfile(file_name, file_size, file_unit='G'):
"""Create a big file.
Args:
file_name: big file name
file_size: big file size
file_unit: big file unit
Returns:
None
"""
unit_dict = {
'k': pow(2, 10),
'm': pow(2, 20),
'g': pow(2, 30)}
if file_unit.lower() not in unit_dict:
sys.exit('Found Errors: %s not found in unit_dict keys.' % (file_unit))
file_unit = file_unit.lower()
file_size *= unit_dict[file_unit]
with open(file_name, 'w+b') as whandle:
# 减去最后一个字符
whandle.seek(file_size-1)
# 写入字符串结束符
whandle.write('\x00')
def main():
"""Main function."""
file_name = 'xmdevops'
create_bigfile(file_name, 10, 'G')
if os.path.exists(file_name):
print 'Found Notice: create file %s success!' % (file_name)
if __name__ == '__main__':
main()