【问题标题】:Opening a File Inside Nested Folders With Glob In Python在 Python 中使用 Glob 打开嵌套文件夹中的文件
【发布时间】:2019-12-06 15:06:58
【问题描述】:

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

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'

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

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

I used this question to get where I am now.

【问题讨论】:

    标签: python file directory


    【解决方案1】:

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

    【讨论】:

    • 谢谢!出于某种原因,我认为我必须先找到文件。
    猜你喜欢
    • 2013-06-03
    • 2015-03-13
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 2015-01-19
    • 2016-03-27
    • 1970-01-01
    相关资源
    最近更新 更多