Python import file with dependencies in the same folder - python

I have a file ref.py that depends on a text file ex.txt, that is in the same directory \home\ref_dir . So it works normally when I run the file ref.py, but if I try to import ref.py to another file work.py in a different directory \home\work_dir , I do the following
import sys
sys.path.append('\home\ref_dir')
import ref
But then I get an error, that the program cannot find ex.txt
How can I solve this issue without using absolute paths. Any ideas?

Use the os module to get access to the current absolute path of the module that you're in, and then get the dirname from that
You would want to open ex.txt in your file like this.
import os
with open('%s/ex.txt' % os.path.dirname(os.path.abspath(__file__)) as ex:
print ex.read()

Related

I try to get to the directory of where the current python script is located [duplicate]

How do I get the current file's directory path?
I tried:
>>> os.path.abspath(__file__)
'C:\\python27\\test.py'
But I want:
'C:\\python27\\'
The special variable __file__ contains the path to the current file. From that we can get the directory using either pathlib or the os.path module.
Python 3
For the directory of the script being run:
import pathlib
pathlib.Path(__file__).parent.resolve()
For the current working directory:
import pathlib
pathlib.Path().resolve()
Python 2 and 3
For the directory of the script being run:
import os
os.path.dirname(os.path.abspath(__file__))
If you mean the current working directory:
import os
os.path.abspath(os.getcwd())
Note that before and after file is two underscores, not just one.
Also note that if you are running interactively or have loaded code from something other than a file (eg: a database or online resource), __file__ may not be set since there is no notion of "current file". The above answer assumes the most common scenario of running a python script that is in a file.
References
pathlib in the python documentation.
os.path - Python 2.7, os.path - Python 3
os.getcwd - Python 2.7, os.getcwd - Python 3
what does the __file__ variable mean/do?
Using Path from pathlib is the recommended way since Python 3:
from pathlib import Path
print("File Path:", Path(__file__).absolute())
print("Directory Path:", Path().absolute()) # Directory of current working directory, not __file__
Note: If using Jupyter Notebook, __file__ doesn't return expected value, so Path().absolute() has to be used.
In Python 3.x I do:
from pathlib import Path
path = Path(__file__).parent.absolute()
Explanation:
Path(__file__) is the path to the current file.
.parent gives you the directory the file is in.
.absolute() gives you the full absolute path to it.
Using pathlib is the modern way to work with paths. If you need it as a string later for some reason, just do str(path).
Try this:
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
import os
print(os.path.dirname(__file__))
I found the following commands return the full path of the parent directory of a Python 3 script.
Python 3 Script:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from pathlib import Path
#Get the absolute path of a Python3.6 and above script.
dir1 = Path().resolve() #Make the path absolute, resolving any symlinks.
dir2 = Path().absolute() #See #RonKalian answer
dir3 = Path(__file__).parent.absolute() #See #Arminius answer
dir4 = Path(__file__).parent
print(f'dir1={dir1}\ndir2={dir2}\ndir3={dir3}\ndir4={dir4}')
REMARKS !!!!
dir1 and dir2 works only when running a script located in the current working directory, but will break in any other case.
Given that Path(__file__).is_absolute() is True, the use of the .absolute() method in dir3 appears redundant.
The shortest command that works is dir4.
Explanation links: .resolve(), .absolute(), Path(file).parent().absolute()
USEFUL PATH PROPERTIES IN PYTHON:
from pathlib import Path
#Returns the path of the current directory
mypath = Path().absolute()
print('Absolute path : {}'.format(mypath))
#if you want to go to any other file inside the subdirectories of the directory path got from above method
filePath = mypath/'data'/'fuel_econ.csv'
print('File path : {}'.format(filePath))
#To check if file present in that directory or Not
isfileExist = filePath.exists()
print('isfileExist : {}'.format(isfileExist))
#To check if the path is a directory or a File
isadirectory = filePath.is_dir()
print('isadirectory : {}'.format(isadirectory))
#To get the extension of the file
fileExtension = mypath/'data'/'fuel_econ.csv'
print('File extension : {}'.format(filePath.suffix))
OUTPUT:
ABSOLUTE PATH IS THE PATH WHERE YOUR PYTHON FILE IS PLACED
Absolute path : D:\Study\Machine Learning\Jupitor Notebook\JupytorNotebookTest2\Udacity_Scripts\Matplotlib and seaborn Part2
File path : D:\Study\Machine Learning\Jupitor Notebook\JupytorNotebookTest2\Udacity_Scripts\Matplotlib and seaborn Part2\data\fuel_econ.csv
isfileExist : True
isadirectory : False
File extension : .csv
works also if __file__ is not available (jupyter notebooks)
import sys
from pathlib import Path
path_file = Path(sys.path[0])
print(path_file)
Also uses pathlib, which is the object oriented way of handling paths in python 3.
IPython has a magic command %pwd to get the present working directory. It can be used in following way:
from IPython.terminal.embed import InteractiveShellEmbed
ip_shell = InteractiveShellEmbed()
present_working_directory = ip_shell.magic("%pwd")
On IPython Jupyter Notebook %pwd can be used directly as following:
present_working_directory = %pwd
I have made a function to use when running python under IIS in CGI in order to get the current folder:
import os
def getLocalFolder():
path=str(os.path.dirname(os.path.abspath(__file__))).split(os.sep)
return path[len(path)-1]
Python 2 and 3
You can simply also do:
from os import sep
print(__file__.rsplit(sep, 1)[0] + sep)
Which outputs something like:
C:\my_folder\sub_folder\
This can be done without a module.
def get_path():
return (__file__.replace(f"<your script name>.py", ""))
print(get_path())

Can't import module from different directory python

So below I have is my file structure:
/
api.py
app.py
code/
data_api/
weather_data/
get_data_dir/
get_data.py
master_call_dir/
master_call.py
My current problem is that I'm importing master_call.py from inside app.py I do this with the following code:
-- app.py
import sys
sys.path.append('./code/data_api/weather_data/master_call_dir')
from master_call import master_call_interface as master_call
Importing master_call itself works perfectly fine, the issue is that inside off master_call I import get_data.py. I do this with the following code:
-- master_call.py
import sys
sys.path.append("../get_data_dir")
from get_data import get_data_module
When printing out sys.path I can see ../get_data_dir inside of it but Python still isn't able to find the get_data.py file in /get_data_dir.
Does anyone know how to fix this?
(there is an __init__.py file in every directory, I removed it here for readability)
So I figured it out. The problem is while the directory of the master_call.py file is /code/data_api/weather_data/master_call the current working directory (CWD) is / since that is where app.py is located.
To fix it we simply fetch absolute file path in each python script with
current_path = os.path.dirname(os.path.realpath(__file__)).replace('\\', '/')
and pre-pend it to any file path we're appending with sys.path.append
So it'd look like this in practice:
import sys
import os
current_path = os.path.dirname(os.path.realpath(__file__)).replace('\\', '/')
sys.path.append(f'{current_path}./code/data_api/weather_data/master_call_dir')
from master_call import master_call_interface as master_call

how to call and execute file in another folders file?

I need to call 'test.txt' file in 'req.py' to execute some data.
File structure:
application - 1.Foldername -- req1.py
2.test.txt
will the same structured will be followed in the azure function app to call text.txt?
#req1.py
file1 = open(r'application/test.txt',"r")
print(file1.readlines())
file1.close()
Yes we can call the files from other folders and can read them, as below:
ReproScreenshot
We can just simply import .py files in the same folder by adding import pythonfile
Below are few ways like how we can include python files.
Including modules:
import sys
# Insert the path of modules folder
sys.path.insert(0, "C:\\Users\\anila\\Desktop\\Task\\modules")
# Import the module0 directly since
# the current path is of modules.
import firstModule
# Prints "Module0 imported successfully"
firstModule.run()
Import method:
import sys
sys.path.insert(0, 'path/to/your/py_file')
import py_file

How to delete all folders inside folder using python?

Is it possible to delete all folders inside a folder without using a specific path?, Here i am moving the contents of the file then after that i want to delete if it is a dir
import os, zipfile
import shutil
import os
from os import path
dir_name = 'C:\\Users\\Guest\\Desktop\\OJT\\samples'
destination = 'C:\\Users\\Guest\\Desktop\\OJT\\scanner\\test'
for path, subdirs, files in os.walk(destination):
for name in files:
filename = os.path.join(path, name)
shutil.copy2(filename, destination)
Yes it is, Use shutil's rmtree method.
import shutil
shutil.rmtree('directory') # the directory you want to remove
os.listdir()
You can also use os.rmdir but that won't work if there is any content in it.
If you want to check if that specific path is a directory then you can use os.path.isdir then run rmtree if that returns TRUE
And incase you want to keep the root folder intact then you could walk that directory and calling rmtree on each item.
As suggested by #Vineeth Sai earlier, If you want to delete all sub directories in a directory, just loop through each file with os.listdir() and if the file is a directory, apply shutil.rmtree():
from os import listdir
from os.path import abspath
from os.path import isdir
from os.path import join
from shutil import rmtree
path = 'YOUR PATH HERE'
for file in listdir(path):
full_path = join(abspath(path), file)
if isdir(full_path):
rmtree(full_path)
The above also uses os.isdir() to check if a file is a directory.
If Vineeth's answer is not suitable for your case, you could you subprocess module to run os specific commands like below
import subprocess
subprocess.call('rm -rf /path/of/the/dirctory/*', shell=True)
The above command is linux specific, you could use windows counterpart of the same command above.
Note - Here shell=True will expand * into files/folders.
Also, note Vineeth's answer is os independent, and above will be os specific. Be cautious.
P.S. - You can also run powershell commands using subprocess module.

python code to get the files outside the current working directory

using python code how can i get the files outside the current working directory
am working with e:\names\test.py directory
dirpath = os.path.dirname(os.path.realpath(__file__))
dirpath prints e:\names
in e:\images
how can i get the path e:\images\imh.png from the file test.py
Am hardcode the above path in test.py,how can i set the relative path inside test.py file
You can use:
os.path.split(os.getcwd())[0] to get the parent directory.
Then, you can use:
a=os.path.split(os.getcwd())[0]
os.listdir(a)
for listing the contents of the parent directory
Also, this works too:-
os.listdir(os.pardir)
import sys
import os
sys.path.insert(1, os.path.split(os.getcwd())[0] + '/directory_name')

Categories