python - 如何使用python获取文件夹中选定文件的路径?
问题描述
我正在使用glob
检索文件夹中所有文件的路径(“数据”):
import glob
import geopandas
for file in glob.glob(r"/home/data/*.txt"):
sf = geopandas.read_file(file)
但是,在这里,我有兴趣知道如何仅检索名称在变量中作为列表列出的选定文件。例如,我只想要以下文件的路径:aaa.txt、bdf.txt、hgr.txt,在“data”文件夹中,它们在变量“imp”中列出。
imp = ['aaa.txt', 'bdf.txt', 'hgr.txt']
解决方案
像这样的东西可以做到。只需遍历您需要的文件。
import geopandas
imp = ['aaa.txt', 'bdf.txt', 'hgr.txt']
for file in imp:
sf = geopandas.read_file(f'/home/data/{file}')
推荐阅读
- c - Redis 为什么会保存部分数据集?
- triggers - 触发器测试类的测试覆盖率不断显示 53
- php - PHP 7.x SQLITE3 PDO - execute() 是否关闭了 PDO 连接?
- submenu - 我们可以在 react-admin 中为用户、帖子和仪表板添加子菜单吗?如果可以,我们如何添加?
- linux - top 命令在linux中是如何工作的?它如何提供实时输出?
- amazon-web-services - 如何在同一 AWS EB 应用程序中托管基于不同版本的路由
- elasticsearch - ElasticSearch 日期范围查询不适用于自定义日期格式
- javascript - 从我的 React 组件中的文本中打印字母,就像旧的视频游戏一样
- ios - 无法添加钥匙串项。错误 - 使用 KeychainItemWrapper 更改标识符后出现 25299?
- javascript - 我们可以同时使用 `export default` 和 `module.exports` 吗?