[英]Opening a File Inside Nested Folders With Glob In 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
但它給了我錯誤
沒有這樣的文件或目錄:'inventory.1.txt'
但我知道 PlayerFiles/PlayerItems 中有'inventory.1.txt'
。
我究竟做錯了什么? 是因為它是一個字符串嗎?
如果你有路徑和文件名,就像你的 join 構造的那樣, 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.