I have a problem with relative paths in my python 2.7 project. I have two files, let's call them script.py and importedScript.py, which have different directories, because importedScript is in a subfolder.
importedScript.py has a method called openCSV(), which gets imported in script.py with
from subfolder.importedScript import openCSV
This works fine. The method openCSV(filename) has the following code inside:
script_path = os.path.dirname(os.path.abspath(__file__))
filepath = os.path.join(script_path, 'subfolder2/' + filename)
dataset = pd.read_csv(filepath)
This code imports a .csv file from a subfolder. This works also fine, if I run the importedScript.py by itself.
The problem now is, that when I run script.py, the relative path in importedScript.py is generated wrong. For some reasons, the system tries to load the importedScript.py from "subfolder2/" instead of "subfolder/subfolder2".
Does anyone know how to fix this?
Edit: In subfolder2 are different .csv files and I want to open different files from different python files.
you can pass the __file__ variable to the method on call:
def OpenCSV(file):
here = os.path.dirname(os.path.abspath(file))
...etc
can be called by doing OpenCSV(__file__)
Related
I have a small question which about the funcion of numpy load.
First I save a Matrix called matrix.npy to a Folder called No_Shadowing using np.save.
I have the following folder structure: scripts -> Plots -> NO_Shadowing
Inside the folder scripts I create a script in which I want to load the saved matrix.
I don't understand which path I should give np.load, so that it loads my file.
Do I need to give the path from scripts, e.g. something like np.load('Plots/No_Shadowing/matrix.np)? I tested all variants I can imagine, none worked.
Depends...if the path is relative to your current folder you can use:
import os
np.load(os.getcwd() + "\\scripts\\Plots\\No_Shadowing\\matrix.np")
or...if scripts is at the root dir.
np.load('c:\\scripts\\Plots\\No_Shadowing\\matrix.np')
I have a file structure that looks like this:
-Project
-trainingData
- Folder1
- Folder2
-python
- get_files.py
-train_model.py
If I want to iterate over each folder in my folder trainingData I use the following function:
data = os.listdir(path_data)
for folder in data:
# Do sth
path_data is "../training_data" in this case. Which makes sense because I need to go one level up and then into the trainingData folder. In cmd I would also type "cd .." and then "cd trainingData"
But why is the path "./training_data" when I call the function in get_files.py from train_model.py. This does not make sense to me. Isn't the relativ path still the same if I would call the function in get_files.py from the file itself?
Because the path is relative to the python file being executed. in this case. train_model.py
The relative path is from the __main__ script, in this case train_model.py. If this wasn't the case then every library or script you imported could all have different relative paths and things would get screwy quickly.
why is the path "./training_data" when I call the function in get_files.py from train_model.py
Because you are calling the file from train_model.py hence the relative path is changed (relative to the current running file).
In order to check the Absolute Path you can use os.path.abspath or os.path.realpath.
Also the newest Path.resolve:
Make the path absolute, resolving any symlinks. A new path object is returned:
I have a folder which contains .py scripts and each contains a same variable which I need in other script if that certain script is used from that folder.
folder_x
main.py
folder_y
script1.py
script2.py
script3.py
So all the scripts are not used at the same time just one of them.
I found this solution https://stackoverflow.com/a/35524184/5708537
And it works well but I have to list all the scripts manually.
I thought that I automate this and make a list of the files, and strip down the .py ending:
path = '/home/folder_x/folder_y'
files = os.listdir(path)
module_list = [i for i in files if i.endswith('.py')]
module_list = [os.path.splitext(x)[0] for x in module_list]
Works like a charm.
But this part of the code still thinks that the scripts are in folder_x
variables = {}
for mod_name in module_list:
mod = import_module(mod_name)
variables[mod_name] = getattr(mod, 'var')
So how can I tell to that the scripts are in folder_y and take that variable from those?
Or is there a better way to list scripts/modules from another folder, and get a variable from each of those?
If you want to import your own .py, just place them in the same directory as the program that is calling it. For example:
from mymodule import *
This lets you run all functions and methods in that file.
Preliminary:
I have Anaconda 3 on Windows 10, and a folder, folder_default, that I have put on the Python path. I'm not actually sure whether that's the right terminology, so to be clear: regardless to where my Python script is, if I have a line of code that says import myFile, that line of code will succeed if myFile.py is in folder_default.
My issue:
In folder_default, I have:
A subfolder called useful_files which contains a text file called useful_file_1.txt.
A python script called my_misc.py.
my_misc.py has a line similar to: np.loadtxt('useful_files/useful_file_1.txt'). This line does not work if I use import my_script in a python file in a location other than folder_default, since useful_files/useful_file_1.txt is not the folder path relative to the python file that imports my_misc.py. I don't want to start using global file paths if I can avoid it.
How can I access files using file paths relative to the imported python module, rather than relative to the python script that imports that module?
Please let me know if the question is unclear - I tried to write a fake, minimal version of the setup that's actually on my computer in the hopes that that would simplify things, but I can change it if that actually makes things more confusing.
Thanks.
You can get the path to current module using the getfile method of inspect module as inspect.getfile(inspect.currentframe()).
For example:
# File : my_misc.py
import os, inspect
module_dir_path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # get path to the directory of current module
useful_file_path = os.path.join(module_dir_path,'useful_files','useful_file_1.txt')
# path stored in useful_file_path. do whatever you want!
np.loadtxt(useful_file_path)
The following code results in the log file printed to different folders depending on whether the test passes or not. I have a test case with one test purpose. During the running of the test, it does a chdir().
If the test result is fail (an assert* fails), the xml file is written to the test's current directory. If the test result is pass, then the xml file is written to the start folder. See the code snippet for how I specify the log file folder. Other than using full paths, is there a way to make python unittest always write it to the start folder?
logFolderName = "TestMyStuff_detail-" +str(scriptPid)
unittest.main(testRunner=xmlrunner.XMLTestRunner(output=logFolderName),
failfast=False)
Other than using full paths, is there a way to make python unittest always write it to the start folder?
Doubtful since relative paths will always be relative to the current working directory. If your test changes the current working directory, you're kind of out of luck.
With that said, it shouldn't be too hard to use a full path:
import os
cwd = os.getcwd()
localLogFolderName = "TestMyStuff_detail-" +str(scriptPid)
logFolderName = os.path.abspath(os.path.join(cwd, localLogFolderName))
you could use a fixed path to write your output.
Something like
path_to_my_output_folder="/path/to/output/"
test1_write_xml(path_to_my_output_folder+"file1.xml")
test2_write_xml(path_to_my_output_folder+"file2.xml")
test3_write_xml(path_to_my_output_folder+"file3.xml")