Import File from File Directory (Python) [duplicate] - python

This question already has answers here:
import module from string variable
(7 answers)
How can I import a module dynamically given the full path?
(35 answers)
Closed 3 years ago.
Currently, I have the directory to a file
"../Model.py"
This model has a class called Test.
Using the string of the directory, I want to import and use the class Test.
How do I do so?
(The String will change dynamically)

From: https://docs.python.org/3/tutorial/modules.html#the-module-search-path
I tested this and I believe it should work for you as well. The code should look as follows:
import sys
directory = '../Model.py'
directory = directory[:-8]
sys.path.append(directory)
from Model import Test

Related

How to get the directory of a function? [duplicate]

This question already has answers here:
Relative paths in Python [duplicate]
(21 answers)
what does the __file__ variable mean/do?
(6 answers)
Closed 1 year ago.
For example:
in the file main/main.py
import core.fun as f
f.script()
and in the file main/core/fun.py:
def script():
f = open('test.txt','r')
where a file test.txt is present in the folder main/core/.
If I run main/main.py it will not work because python will search for the file 'test.txt' in the main folder and not in the main/core folder.
And if I call within the function script()
import os
os.getcwd()
It will again return the directory main
Therefore I would like to know how can I find the directory of the function within the function.

Python: Read only file name, instead of path [duplicate]

This question already has answers here:
Extract file name from path, no matter what the os/path format
(22 answers)
Closed 5 years ago.
How to get ONLY filename instead of full path?
For example:
path = /folder/file.txt
and i need to get:
filename = file.txt
How to do that?
You should use the os module:
import os
filename = os.path.basename(path)
For other path manipulations look here (for python 2.7) or here (for python 3)

How to call a function if the function's file name is known and is a string? [duplicate]

This question already has answers here:
How can I import a module dynamically given its name as string?
(10 answers)
Closed 7 years ago.
I am scanning a directory for new python scripts.
I expect each script file to have an arbitrary function report().
Say, the following files were found in my directory: ['file1.py', 'file2.py'].
So "file1.py" should contain:
def report():
print('I am just a script.')
I need to call report() function for each one of them.
How to do it?
You can use the builtin function __import__ to do a dynamic import, something like this:
for file in files:
mod = __import__(file)
mod.report()
Note - you will need to strip the '.py' extension from the filename, and this will be made more complicated if the current working directory is not on the python path.
This SO answer has some more detail on __import__(): Dynamic module import in Python. If you need to load from somewhere off the python path, then look at the second answer

How to change file extension with Python? [duplicate]

This question already has answers here:
Extracting extension from filename in Python
(33 answers)
Closed 7 years ago.
I'm trying to make a program that will take a file, say my_test_file.log and make a new file called my_test_file.mdn. I'd like to be able to use this program by typing python renameprogram.py my_test_file.log into the command line. The original file will always end in .log.
from shutil import copyfile
from glob import glob
map(lambda x:copyfile(x,x[:-3]+"mdn"),glob("*.log"))
or perhaps more simply
...
import sys
copyfile(sys.argv[1],sys.argv[1][:-3]+"mdn")
You certainly can create a Python program that will accomplish this, but there are shell level commands that already do this.
For Linux/Unix:
mv my_test_file.log my_test_file.mdn
For Windows (see this link):
rename my_test_file.log my_test_file.mdn

How to find what files are in a folder [duplicate]

This question already has answers here:
Directory-tree listing in Python
(21 answers)
Closed 9 years ago.
I am writing a function that is recieving a folder path as an arguemnt. I want her to add into a dictionary what's inside the folder (like dir in CMD)
How can I do this ?
Thank you in advance,
Iliya
import os
print os.listdir('/tmp')
Similar Topics:
Directory listing in Python
Also, I use os.path and glob a lot while manipulating file system path.
You might want to check it out.

Categories