【问题标题】:Create list of files names of certain file types, if folder does not contain file type, print this statement创建某些文件类型的文件名列表,如果文件夹不包含文件类型,则打印此语句
【发布时间】:2021-05-02 07:03:38
【问题描述】:

我正在从文件夹中特定类型的文件创建文件名列表:

items = os.listdir(r'E:/folder/test')

print('checking directory...')
print('your files are: ')
print()
filenames = []
for names in items:
    if names.endswith(".shp"):
        filenames.append(names)
        print(filenames)
        print()
    else:
        print("there are no files ending with .shp in your folder")

如果文件夹中没有文件是 .shp 文件,我希望 if/else 条件打印一条说明这一点的语句。我的代码目前正在做这两个,有什么解决方案吗?谢谢!

【问题讨论】:

    标签: list file if-statement directory file-type


    【解决方案1】:

    这是正确的代码:

    items = os.listdir(r'E:/folder/test')
    
    print('checking directory...')
    print('your files are: ')
    print()
    filenames = []
    
    for names in items:
        if names.endswith(".shp"):
            filenames.append(names)
            print(filenames)
    
    if not filenames: 
        print("there are no files ending with .shp in your folder")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多