python - Getting the current path from where a module is being called -
i have python module built myself , it's located in /usr/local/lib/python3.4/site-packages/my_module1. in module have this:
class class1(object); def __init__(self, file_name): self.file_name = file_name # how can full path of file_name? how full of file_name? is, if it's file name without path, append current folder from module being called. otherwise, treat file name full path.
# /users/me/my_test.py module1 import class1 c = class1('my_file1.txt') # should become /users/me/my_file1.txt c1 = class1('/some_other_path/my_file1.txt') # should become /some_other_path/my_file1.txt/users/me/my_file1.txt
update: sorry that. mis-read question. need pass filename os.path.abspath().
example:
import os filename = os.path.abspath(filename) to cater 2nd case (which find quite weird):
import os if os.path.isabs(filenaem): filename os.path.join( os.path.join( filename, os.getcwd() ), os.path.basename(filename) )