简体   繁体   English

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

[英]Opening a File Inside Nested Folders With Glob In Python

I am trying to open a file inside the two folders我正在尝试打开两个文件夹中的文件

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

But it is giving me there error但它给了我错误

There is no such file or directory: 'inventory.1.txt'没有这样的文件或目录:'inventory.1.txt'

But I know for a fact that there is 'inventory.1.txt' inside PlayerFiles/PlayerItems.但我知道 PlayerFiles/PlayerItems 中有'inventory.1.txt'

What am I doing wrong?我究竟做错了什么? Is it because it is a string?是因为它是一个字符串吗?

I used this question to get where I am now. 我用这个问题来了解我现在的位置。

If you have the path and the filename, as constructed with your join, what is glob doing there?如果你有路径和文件名,就像你的 join 构造的那样, glob在那里做什么? It looks like you're opening a single file.看起来您正在打开一个文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM