Import entire file in module - python

I have a folder structure like so:
main/
__init__.py
files/
__init__.py
a.csv
b.yml
subfolder/
__init__.py
code.py
In code.py, I want to use the files in the files folder as so:
# HOW DO I IMPORT a.csv and b.yml
# from main.files import a -- this doesn't work
class Code():
def func(file_a, file_b):
# do something
Is this something I can do? How do I do this?

If code.py is in the same folder as your files (.csv and .yml) you don't have to import them instead you can open them and read them within the function like so:
import csv
import yaml
file_a = 'a.csv'
file_b = 'b.yaml'
class Code():
def func(file_a, file_b):
with open(file_a) as f_obj:
read = csv.reader(f_object)
# do something with opened file
with open("example.yaml", 'r') as stream:
another_read = yaml.safe_load(stream))
# do something with opened file
I think your second file is supposed to be a .yaml file not a .yml file.

Related

calling a python module that reads a file

so my program import a utils that reads a file in the same directory as the utils. However, this utils function can be called from different files from different directory.
Project
|
|-module_1:
|__ init __.py
| file.py <--- calls util.load_file()
|module_2:
| __ init __.py
| utils.py <---- load_file() path used 'file.txt'
| file.txt
what is this thing called ? I couldn't even search for it. tried package managment, expanding path ...etc
__file__ contains the path to the current file. Check it with print(__file__).
pathlib from Pythons standard library can be used to construct an absolute path to the data file.
import pathlib
print(pathlib.Path(__file__))
print(pathlib.Path(__file__).parent)
print(pathlib.Path(__file__).parent / 'file.txt')
You can now open your file like this:
filepath = pathlib.Path(__file__).parent / 'file.txt'
with open(filepath) as f:
for line in f:
print(line)

Reading a file from the directory which module is imported

I am trying to read a file in a custom module I have created in Python. But it is showing error when I try to do so.
Directory structure
base/
|
|_ app.py
|_ cmod/
|
|_ __init__.py
|_ util.py
|_ db.csv
util.py snippet
from csvhandler import CSVFile
def get_db():
with open("db.csv", "r+") as db:
data = CSVFile(db)
return data
app.py snippet
from cmod import util
data = util.get_db()
It throws the following error
FileNotFoundError: [Errno 2] No such file or directory: 'db.csv'
Is there any issue with imports of placement?
Note: The db.csv has to be put there only.
The current working directory when you run this is likely the one app.py sits in. open() looks for files in the current working directory, not the directory of the currently executing file.
The most portable way to open a file stored relatively would be to get the full path to the python file, isolate the directory, and then join it.
util.py
import os
def get_db():
directory = os.path.dirname(__file__)
with open(os.path.join(directory, "db.csv"), "r+") as db:
data = CSVFile(db)
return data

Get a file from the same folder when importing (without hardcoding the path)

I'd like to know how I can access important_file.txt without hardcoding the path.
I run main.py that imports some code from myfile.py, where I need to read important_file.txt
Any ideas on how I can do this?
Files:
main.py
mydir/
mydir2/
myfile.py
important_file.txt
main.py
from mydir.mydir2.myfile import MyClass
MyClass.dostuff()
mydir/mydir2/myfile.py
class MyClass:
def dostuff():
pass
# something to read important_file.txt
# without hardcoding it (writing mydir/mydir2/important_file.txt)
Command
python3 main.py
You can use __file__ to read the path of your module. Since myfile.py and important_file.txt are on the same directory you can then use os.path.dirname to get the name of the directory and join it with your file name to get an absolute path to your .txt file. In your myfile.py do:
import os
class MyClass:
def dostuff():
dirname = os.path.dirname(__file__)
with open(os.path.join(dirname, "important_file.txt")) as f:
print(f.read())

import python files from another dierctory

I've tried too many solutions that are provided here on SO but wasn't able to make this work.
here is directory structure.
|project
|------->folder1
|-------------->file1.py #file1.py contains class File1
|-------------->file2.py #file2.py contains class File2
|------->folder2
|-------------->another_file1.py #another_file1.py contains class SomeClass1
|-------------->another_file2.py #another_file2.py contains class SomeClass2
|------->Runner
|-------------->logic.py
I need to create instance of all the classes here in logic.py
I created __init__.py in all folders, and one in the main project.
__init__.py # folder1
from . import file1
from . import file2
__init__.py # folder2
from . import another_file1
from . import another_file2
__init__.py # project
from . import folder1
from . import folder2
I'm unable to import any of stuff inside Runner.
I get following errors.
No module named file1.
No module named file2.
SystemError: Parent module '' not loaded, cannot perform relative import
However I can make it to work by this tweak.
import sys
sys.path.append('../')
in my files inside Runner directory. But I don't want to do this in my every file.
P.S. using Python 3.5
Anyone coming across the same issue, can do something like this.
1) create new project e.g: StreamingApplication.
|root --StreamingApplication
|-------> utils
|-------------->helper.py
|-------------->storage_handler.py
|------->config
|-------------->config.py
|-------------->hook.py
|------->app
|-------------->application.py
So, you can also create __init__.py file if you want to mark folder as python package.
add project root path to PYTHONPATH system variable.
in ~/.bashrc or /etc/profile
export PYTHONPATH=/path/to/StreamingApplication
source file with source ~/.bashrc or source /etc/profile
then in application.py you can do something like this.
from utils.helper import write_file #write_file is function.
from utils.storage_handler import StorageHandler # StorageHandler is class
import config as conf
class App:
def __init__(self):
self.executors = conf.executors # access executors variable from config
All set.

Create a utility function that retrieves the user-executed script's dir

There are multiple threads about getting the current Python's script directory, for example:
import os
dir = os.path.dirname(os.path.abspath(__file__))
The question is, what should I do if I want to add this function into some utility file, while I want the returned value to be the directory of the calling file, in this case, the file executed by the user?
What would happen here?
// utils.py
def get_script_dir():
import os
return os.path.dirname(os.path.abspath(__file__))
// main_app.py
from utils import get_script_dir
print(get_script_dir())
// user's shell
python c:\path\to\somewhere\main_app.py
Will it print the directory of main_app.py or the directory of utils.py? What is an elegant solution for placing the function in the utils file while getting the directory of the file that was actually executed by the user?
Please try following and let me know whether it's exactly what you want:
# utils.py
def get_script_dir():
import sys
import os
return os.path.dirname(sys.modules['__main__'].__file__)
# main_app.py
from utils import get_script_dir
print(get_script_dir())
Supposing that your project directory tree is like the following:
Python/
main.py
modules/
utils.py
__init__.py
Being Python/modules/utils.py:
import os
get_script_dir = lambda file: os.path.dirname(os.path.abspath(file))
and Python/main.py:
from modules import utils
import os
script_dir = utils.get_script_dir(__file__)
print("[i] Currently executing file {} located at {}".format(os.path.basename(__file__), script_dir))
Executing Python/main.py is going to output something like:
[i] Currently executing file main.py located at C:\Users\BlackVirusScript\Desktop\Python

Categories

Resources