【问题标题】:Write on a file the volume of a drive plus random text在文件上写入驱动器的卷加上随机文本
【发布时间】:2020-03-29 13:03:03
【问题描述】:

我正在尝试在 E 盘的卷上写入。但在那个文件中,我还想添加一些随机文本,比如“随机文本”。 我该怎么做?

代码如下:

import os.path
textFile=os.system("vol E: > file.txt")

【问题讨论】:

    标签: python python-3.x path operating-system drive


    【解决方案1】:

    要生成随机文本,您可以导入 random 模块,然后制作一个随机单词或字符列表,然后将它们添加在一起

    import random
    import os
    
    words = ['dog', 'pizza', 'pasta', 'Giovanni', 'lasagne', 'salame', 'pecorino']  # make a list of words or characters
    
    minimum_words = 3
    maximum_words = 10
    random_text = ''
    for i in range(random.randint(minimum_words, maximum_words)): # uses as range a random integer number between 3 and 10
        random_text += random.choice(words) + ' ' # choose a random word from the list
    
    os.system(f"vol E: {random_text} > file.txt") # use f strings
    

    希望这能解决你的问题,晚安

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 2016-08-28
      • 2016-09-06
      • 1970-01-01
      • 2022-07-01
      • 2021-06-02
      相关资源
      最近更新 更多