我自己构建了一个python模块,它位于/usr/local/lib/python3.4/site-packages/my_module1
。在模块中,我有:
class Class1(object);
def __init__(self, file_name):
self.file_name = file_name # how can I get the full path of file_name?
我怎样才能得到满满的file_name
?也就是说,如果只是一个没有路径的文件名,那么在调用模块的地方附加当前文件夹。否则,将文件名视为完整路径。你知道吗
# /users/me/my_test.py
from module1 import Class1
c = Class1('my_file1.txt') # it should become /users/me/my_file1.txt
c1 = Class1('/some_other_path/my_file1.txt') # it should become /some_other_path/my_file1.txt/users/me/my_file1.txt
更新:很抱歉。我看错了你的问题。你只需要传递
filename
所以os.path.abspath()
。你知道吗示例:
为了迎合你的第二个案子(我觉得很奇怪):
相关问题 更多 >
编程相关推荐