[英]Reading from a data file
I am trying to read from a txt file I am including with my pyqt application. 我正在尝试从pyqt应用程序包含的txt文件中读取内容。
The problem I am having is that the os.path.dirname(__file__)
is returning the directory where the module that houses finding the data file exists not the directory where main() lives. 我遇到的问题是
os.path.dirname(__file__)
返回的目录中存在查找数据文件的模块,而不是main()所在的目录。 In this case a subdirectory of the application directory not the application directory. 在这种情况下,应用程序目录的子目录不是应用程序目录。
Is there a way to always return the directory where main()
is being called from ? 有没有办法总是返回从其中调用
main()
的目录?
That or can I get the directory to this text file in main() and pass it to Mainform() somehow ? 那或者我可以在main()中获得该文本文件的目录,然后以某种方式将其传递给Mainform()吗?
The reason for all this is because I want to put the .txt in the same directory as main.py so when I deploy my frozen application the text file can be found. 所有这些的原因是因为我要将.txt与main.py放在同一目录中,因此当我部署冻结的应用程序时,可以找到该文本文件。 (on OSX and WIN)
(在OSX和WIN上)
Thanks 谢谢
A setup like this may work for you: 这样的设置可能适合您:
main.py main.py
import other
def main():
print other.get_data_files_dir()
main()
other.py 其他
import os
import sys
def get_data_files_dir():
namespace = sys._getframe(1).f_globals # caller's globals
return os.path.dirname(namespace['__file__'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.