How can I iterate over files in a given directory?

前端 未结 9 884
北海茫月
北海茫月 2020-11-22 04:13

I need to iterate through all .asm files inside a given directory and do some actions on them.

How can this be done in a efficient way?

9条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 05:08

    You can use glob for referring the directory and the list :

    import glob
    import os
    
    #to get the current working directory name
    cwd = os.getcwd()
    #Load the images from images folder.
    for f in glob.glob('images\*.jpg'):   
        dir_name = get_dir_name(f)
        image_file_name = dir_name + '.jpg'
        #To print the file name with path (path will be in string)
        print (image_file_name)
    

    To get the list of all directory in array you can use os :

    os.listdir(directory)
    

提交回复
热议问题