如何从 Python 中的目录中获取文件?

巨蟒书呆子

我在 python 中声明一个变量:

e = ('file.txt', 'r')

但是该文件在一个目录中,我不知道如何编写它以便它会在我的目录 playergold 中查找。我试过了

playergold = 'PlayerFiles/PlayerGold'
e = open('playergold/gold.1.txt', 'r')

e = open(from playergold import gold.1.txt', 'r')

e = open(playergold/'gold.1.txt', 'r')

但两者都不起作用。(顺便说一句,我从问题Loop Through A Folder in Python 中得到了这段代码

目录树如下所示:

代码包含methods.py,这是所有代码所在的地方。

PlayerFiles 包含文件夹 PlayerGold 和 PlayerItems

PlayerGold 包含 gold.1.txt

PlayerFiles 和 Code 处于同一级别。

你能帮忙的话,我会很高兴。谢谢你的时间!

拉克什

看起来你需要。

import os

with open(os.path.join('PlayerFiles','PlayerGold', 'gold.1.txt')) as infile:
    data = infile.read()
    #process

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章