Python – 从文件夹中读取所有文件(.shp,.dbf,.mxd等)

谁能帮我?我正在尝试编写一个代码来读取数据文件夹中的所有文件.这些文件都有不同的扩展名:.shp,.dbf,.sbx,.mxd)我正在使用
Windows.谢谢.

我有:

import os    
path=r'C:\abc\def\ghi\'    
folderList = os.listdir(path)

现在我需要读取文件夹中的所有文件,所以我知道我需要类似的东西

f.open(路径)?

最佳答案 你是在正确的道路上:

import os
path = r'C:\abc\def\ghi'  # remove the trailing '\'
data = {}
for dir_entry in os.listdir(path):
    dir_entry_path = os.path.join(path, dir_entry)
    if os.path.isfile(dir_entry_path):
        with open(dir_entry_path, 'r') as my_file:
            data[dir_entry] = my_file.read()
点赞