zoukankan      html  css  js  c++  java
  • Python在指定文件夹生成随机文件

    Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。
    str = "-";
    seq = ("a", "b", "c"); # 字符串序列
    print str.join( seq );

    Python标准库中的random函数,可以生成随机浮点数、整数、字符串,甚至帮助你随机选择列表序列中的一个元素,打乱一组数据等

    random.sample()可以从指定的序列中,随机的截取指定长度的片断,不作原地修改。

    Python3中String模块ascii_letters和digits方法,其中ascii_letters是生成所有字母,从a-z和A-Z,digits是生成所有数字0-9.

    random.randint()随机生一个整数int类型,可以指定这个整数的范围,同样有上限和下限值,python random.randint。

    python open() 函数用于打开一个文件,创建一个 file 对象,相关的方法才可以调用它进行读写。

    w+ 打开一个文件用于读写。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。

    f.write("hello ") #如果要写入字符串以外的数据,先将他转换为字符串.

    f.close() 关闭文件

    python range() 函数可创建一个整数列表,一般用在 for 循环中。

    https://blog.csdn.net/xyisv/article/details/79148920  Python在指定文件夹生成随机文件

    import random
    import string
    for i in range(10000):
        a=''.join(random.sample(string.ascii_letters+string.digits,random.randint(3,9)))
        f=open("/home/tqhy/backup244/"+a+".txt","w+")
        f.write(''.join(random.sample(string.ascii_letters+string.digits,random.randint(9,19))))
        f.close()
  • 相关阅读:
    27 mysql主从出现错误
    Spring各个jar包作用
    SpringBoot 的启动banner生成网址
    Joda-Time 简介
    IDEA配置GIT
    iView 发布后台管理系统 iview-admin
    Springboot的默认定时任务——Scheduled注解
    如何使用java validation api进行参数校验----Hibernate-Validation
    Vue的安装及使用快速入门
    springboot整合shiro应用
  • 原文地址:https://www.cnblogs.com/lely/p/10364310.html
Copyright © 2011-2022 走看看