Based on your answer to my comment, what you want is to use a relative path to the file, not the absolute path to the file.
I recommend you to use os.path.relpath
to compute the relative path from your current working directory to the file :
import os.path
# inputs
absolute_path_to_file = r"C:\example\cwd\mydir\myfile.txt"
current_working_directory = os.path.abspath(os.path.curdir) # C:\example\cwd\my_other_dir
# using os.path.relpath
relative_path_from_current_working_directory_to_file = \
os.path.relpath(absolute_path_to_file, current_working_directory)
# result
print(relative_path_from_current_working_directory_to_file) # ..\mydir\myfile.txt
If you want, you can omit the second parameter to os.path.relpath
(named start
) because it defaults to your current directory (see os.curdir
), so that you can simply do :
relative_path_from_current_working_directory_to_file = \
os.path.relpath(absolute_path_to_file)