Automatically creating a list in Python

This question already has an answer here:

  • How do I list all files of a directory? 31 answers

  • import glob
    my_new_list = glob.glob("/your/folder/*")
    

    你也可以使用os

    import os
    from random import shuffle
    
    # Base directory from which you want to search    
    base_dir = "/path/to/whatever"
    
    # Only take the files in the above directory
    files = [f for f in os.listdir(base_dir) 
             if os.path.isfile(os.path.join(base_dir, f))]
    
    # shuffle (in place)
    shuffle(files)
    
    链接地址: http://www.djcxy.com/p/20052.html

    上一篇: 我如何自动打开给定文件夹中的所有文本文件?

    下一篇: 在Python中自动创建一个列表