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

2024-06-10 03:35:49 发布

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

我试图打开两个文件夹中的一个文件

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.


Tags: 文件pathimporttxt文件夹oswithopen
1条回答
网友
1楼 · 发布于 2024-06-10 03:35:49

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

相关问题 更多 >