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

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.

Related

printing all the ".lnk" filenames in foldes and sub folders [duplicate]

This question already has answers here:
Extract file name from path, no matter what the os/path format
(22 answers)
Closed 2 years ago.
so there are alot of files with .lnk extension in the start menu folder C:\ProgramData\Microsoft\Windows\Start Menu\Programs i want to print all those file names so i tried this code:
import os
import glob
startmenu = r'C:\ProgramData\Microsoft\Windows\Start Menu\Programs'
os.chdir(startmenu)
for file in glob.glob("**/*.lnk", recursive = True):
print(file)
it prints the link to the files, but i want to print only the file names with the extension of ".lnk"
Convert the absolute path to list then take the last element from the list. See the below code.
import os
import glob
startmenu = r'C:\ProgramData\Microsoft\Windows\Start Menu\Programs'
os.chdir(startmenu)
for file in glob.glob("**/*.lnk", recursive=True):
print(os.path.split(file)[-1])

How to get the path of the file and not the current working directory [duplicate]

This question already has answers here:
How do I get the path and name of the file that is currently executing?
(26 answers)
Closed 3 years ago.
So I have a python file sample.py which is in let's say 'A/B/C' and my current working directory is let's say 'D/E'. Now in sample.py I am needed it's directory that is 'A/B/C' . Can anyone help me with this. I have tried "dir_path = os.path.dirname(os.path.realpath('__sample__'))" but it returns the current working directory.
Change the line:
dir_path = os.path.dirname(os.path.realpath('__sample__'))
to
dir_path = os.path.dirname(os.path.realpath(__file__))
This should work for you. Find more about __file__ here.

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

Recursive os.listdir? [duplicate]

This question already has answers here:
Python recursive folder read
(16 answers)
Closed 9 years ago.
I'd like to get a list of all files in a directory recursively, with no directories.
Say there's a directory ~/files with "a.txt", "b.txt", and a directory "c" with "d.txt" and "e" inside it, and "f.txt" inside e. How would I go about getting a list that looks like ['/home/user/files/a.txt', '/home/user/files/b.txt', '/home/user/files/c/d.txt', '/home/user/files/c/e/f.txt']?
import os
[os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser("~/files")) for f in fn]

Categories