Choose a file starting with a given string

前端 未结 6 834
故里飘歌
故里飘歌 2020-12-13 12:38

In a directory I have a lot of files, named more or less like this:

001_MN_DX_1_M_32
001_MN_SX_1_M_33
012_BC_2_F_23
...
...

In Python, I ha

6条回答
  •  一向
    一向 (楼主)
    2020-12-13 13:08

    You can use the os module to list the files in a directory.

    Eg: Find all files in the current directory where name starts with 001_MN_DX

    import os
    list_of_files = os.listdir(os.getcwd()) #list of files in the current directory
    for each_file in list_of_files:
        if each_file.startswith('001_MN_DX'):  #since its all type str you can simply use startswith
            print each_file
    

提交回复
热议问题