代码之家  ›  专栏  ›  技术社区  ›  RustyShackleford

如何跳过文件夹中没有数据的文件?[复制品]

  •  -2
  • RustyShackleford  · 技术社区  · 7 年前

    这个问题已经有了答案:

    我有一个文件夹,有1000个txt文件。如果文件中有数据,我的代码工作得很好。

    在我的代码中有没有构建逻辑来检查文件是否为空,如果为空则跳到下一个?

    import glob
    
    import os
    
    
    path = 'path/to/file'
    
    
    for filename in glob.glob(os.path.join(path, '*.txt')):
      with open(filename, 'r',encoding='ISO-8859-1') as f:
        print(filename)
        text = f.read()
        do other stuff
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Kevin    7 年前

    if not text: continue