【问题标题】:skip function if folder is empty如果文件夹为空,则跳过功能
【发布时间】:2021-12-01 00:17:37
【问题描述】:

我想读取文件夹名称(A/B/C/D)并为 Files 文件夹中的每个文件调用可调用函数,然后处理下一个文件(自动传递到下一个文件和函数)。 如果文件夹为空,现在我想跳过该功能,请问如何将此条件添加到我的代码中

这是我的代码:

base_path = 'Files/'
for name in os.listdir(base_path):
    path = os.path.join(base_path, name)
    if os.path.isdir(path):
        files = [os.path.join(path, f) for f in os.listdir(path)]
        if name_to_func[path].__code__.co_argcount == len(files):
            name_to_func[path](*files)
        else:
            name_to_func[path](files)
    else:
        name_to_func[path](path)

【问题讨论】:

    标签: python-3.x path arguments


    【解决方案1】:

    如果目录为空,可以这样检查:

    if len(os.listdir(path)) == 0:
        # Directory is empty
    else:    
        # Directory is not empty
    

    【讨论】:

      猜你喜欢
      • 2019-12-26
      • 2014-12-12
      • 2019-03-31
      • 2014-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-27
      相关资源
      最近更新 更多