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

在Python中使用Glob打开嵌套文件夹中的文件,python,file,directory,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 但这给了我错误 没有这样的文件或目录:“inventory.1.txt” 但我知道,在p

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

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
但这给了我错误

没有这样的文件或目录:“inventory.1.txt”

但我知道,在playerFile/PlayerItems中有
'inventory.1.txt'

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


如果你有路径和文件名,就像你的连接一样,那么
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

谢谢出于某种原因,我认为我必须先找到文件。