Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想将 Python 中给定目录中扩展名为 .txt 和 .scv 的所有文件添加到列表中。我当前的代码如下所示:
import glob def core(): old_path = '/home/demo/' files = glob.glob(old_path+(*.{txt, scv})) print(len(files))
但它不起作用。
您的代码中存在语法错误 - 通配符必须是字符串。第二个问题是它glob不支持大括号扩展(即它不理解{txt, csv})。所以你需要使用基本的通配符:
glob
{txt, csv}
import glob def core(): old_path = '/home/demo/' txt_files = glob.glob(old_path+'*.txt') csv_files = glob.glob(old_path+'*.csv') print(len(txt_files + csv_files)) core()