python - 在 Python 中使用 Glob 打开嵌套文件夹内的文件

标签 python file directory

我正在尝试打开两个文件夹中的文件

import glob
import os
wPlayer = '1'
playeritems = 'PlayerFiles/PlayerItems'
with glob.glob(os.path.join(playeritems, open('inventory.%s.txt' % wPlayer, 'r'))) as wPs:
  #do stuff with wPs

但是它给了我错误

There is no such file or directory: 'inventory.1.txt'

但我知道 PlayerFiles/PlayerItems 内有 'inventory.1.txt'

我做错了什么?是因为它是一个字符串吗?

I used this question to get where I am now.

最佳答案

如果您有通过连接构造的路径和文件名,那么 glob 在那里做什么?看起来您正在打开一个文件。

import os
wPlayer = '1'
playeritems = 'PlayerFiles/PlayerItems'
with open(os.path.join(playeritems,'inventory.%s.txt' % wPlayer), 'r') as wPs:
  #do stuff with wPs

关于python - 在 Python 中使用 Glob 打开嵌套文件夹内的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59215650/

相关文章:

Android - 加载文件夹中的所有文件

python - 没有合适的传播模型可用

python - python中噪声数据的线性变换

Python、Numpy、OpenCV——创建修改后的(同样快的) "addWeighted"函数

c# - 我应该如何存储用户指定的一组文件?

c# - 使用 C# 阻止访问目录

python - 替换python中的特殊字符

python - 如何将列表 "one by one"写入 python 中的二进制文件?

java - 从 jsf 页面读取两个日期并仅搜索具有该日期的文件

java - Vista 的全局应用程序数据放在哪里?