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

2024-04-25 00:18:09 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我有:

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

现在我需要阅读文件夹中的所有文件,所以我知道我需要

f.open(path)


Tags: 文件path代码import文件夹oswindowsdef
1条回答
网友
1楼 · 发布于 2024-04-25 00:18:09

你走对了路:

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()

相关问题 更多 >