每日一题.PYTHON如何模拟LINUX的dd命令快速创建大文件?

原文数据:

具体要求:

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()


转载于:https://my.oschina.net/pydevops/blog/618216

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值