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

use*_*713 3 python arcgis

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

我有:

import os    
path=r'C:\abc\def\ghi\'    
folderList = os.listdir(path)
Run Code Online (Sandbox Code Playgroud)

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

f.open(path)

Rus*_*hal 7

你是在正确的道路上:

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()
Run Code Online (Sandbox Code Playgroud)

  • Os.path.join(path,f)会好得多,它是跨平台的.另外,如果你为循环变量分配有意义的名字也不会有害:) (3认同)