在Python中自动创建一个列表

这个问题在这里已经有了答案:

  • 如何列出目录的所有文件? 31个答案

  • 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/20051.html

    上一篇: Automatically creating a list in Python

    下一篇: How do i use Linux terminal commands like CD and LS?