I want to create a tool called unifile
for saving and opening files
like this unifile.open.yaml("file.yaml")
.
This is my structure:
JavaScript
x
9
1
unifile
2
|
3
├-open
4
| └--__init__.py
5
|
6
└-save
7
└--__init__.py
8
9
Code that call my module:
JavaScript
1
3
1
import unifile
2
a = unifile.open.yaml("file.yaml")
3
open/init.py
JavaScript
1
11
11
1
import yaml
2
class open():
3
def yml(self, file_path):
4
try:
5
with open(file_path, "r", encoding="utf-8") as yaml_conf:
6
yaml_file = yaml.safe_load(yaml_conf)
7
8
return yaml_file
9
except OSError:
10
print("Can't load yaml")
11
1 error if I import unifile
always say:
module unifile has no atribute open
2 error in __init__.py
I can’t open file
[pylint] Context manager ‘open’ doesn’t implement enter and exit. [not-context-manager]
Advertisement
Answer
here adding solution to ur problem, make your project structure like this.
add unifile/__init__.py
file in the unifile itself not in other modules.
then unifile/open/_open.py
file content
JavaScript
1
14
14
1
import yaml
2
3
class Open():
4
def __init__(self):
5
pass
6
def yml(self, file_path):
7
try:
8
with open(file_path, "r", encoding="utf-8") as yaml_conf:
9
yaml_file = yaml.safe_load(yaml_conf)
10
11
return yaml_file
12
except OSError:
13
print("Can't load yaml")
14
content of the unifile/__init__.py
file
JavaScript
1
2
1
from .open._open import Open
2
in terminal run the program like this
Also, It is better to create a object element first then proceed ahead.