[英]How to Give Module Access to Non-Python File
Suppose I have the following directory structure:假设我有以下目录结构:
lib/
main.py
module1/
__init__.py
class1.py
foo.txt
My class1.py
file defines a class that has a function that reads the text file.我的class1.py
文件定义了一个 class,它有一个读取文本文件的 function。 My main.py
creates an object from the class defined in class1.py
and attempts to use the function to read the text file in the module1
folder.我的 main.py 从 class1.py 中定义的main.py
创建一个class1.py
并尝试使用 function 来读取module1
文件夹中的文本文件。 However, when I run the main.py file, it says that it can't find the foo.txt
file.但是,当我运行 main.py 文件时,它说找不到foo.txt
文件。 How do I give main.py
access to foo.txt
through class1.py
?如何通过class1.py
授予main.py
访问foo.txt
的权限?
When you call class1.py
from main.py
, your working directory is lib/
(or wherever you called main.py
from) and not module1
.当你从main.py
调用class1.py
时,你的工作目录是lib/
(或者你调用main.py
的任何地方)而不是module1
。
This means that if you want to access foo.txt
you need to specify the path from the working directory , not from where the class1.py
is located.这意味着如果你想访问foo.txt
你需要从工作目录指定路径,而不是从class1.py
所在的位置。
Assuming that you run main.py
from directory lib/
:假设您从目录lib/
运行main.py
:
# class1.py
class Foo():
def read1(self):
try:
f = open('foo.txt')
print("Successfully opened 'foo.txt'.")
print(f.read())
except OSError:
print("Error when trying to read 'foo.txt'.")
def read2(self):
try:
f = open('module1/foo.txt')
print("Successfully opened 'module1/foo.txt'.")
print(f.read())
except OSError:
print("Error when trying to read 'module1/foo.txt'.")
if __name__ == '__main__':
foo = Foo()
foo.read1()
foo.read2()
# __init__.py
from module1.class1 import Foo
# main.py
from module1 import Foo
foo = Foo()
foo.read1()
foo.read2()
module1
and run class1.py
:如果您在module1
内并运行class1.py
:Successfully opened 'foo.txt'.
...file contents...
Error when trying to read 'module1/foo.txt'.
As you can see read1
worked and not read2
.如您所见, read1
有效,而不是read2
。
lib/
and call main.py
, you'll get:如果你在lib/
中并调用main.py
,你会得到:Error when trying to read 'foo.txt'.
Successfully opened 'module1/foo.txt'.
...file contents...
This time read2()
worked and not read1()
.这次read2()
有效,而不是read1()
。
Notes:笔记:
class1.py
directly from lib/module1
then read_file_1()
will work instead of read_file_2()
.如果您直接从lib/module1
module1 调用class1.py
,那么read_file_1()
将代替read_file_2()
工作。I like to use the __file__
attribute of the module.我喜欢使用模块的__file__
属性。 Since you know where foo.txt
is located with respect to class1.py
, you can simply use由于您知道foo.txt
相对于class1.py
的位置,因此您可以简单地使用
# inside class1.py
from pathlib import Path
file = Path(__file__).resolve().parent / 'foo.txt'
This will work always, no matter where you call the script, or what script is the entry point.这将始终有效,无论您在哪里调用脚本,或者入口点是什么脚本。
__file__
attribute is not anymore pointing to a .py
file.如果您计划使用 pyinstaller 或类似工具构建和执行,则需要找到一种解决方法,因为__file__
属性不再指向.py
文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.