I need to list absolute paths to files in a directory for python 3.7.5
. Now the issue is that neither of the solutions specified here work:
Get absolute paths of all files in a directory
Perhaps because I log in to a different user and my home directory changes: sudo su - my_user_name
Such solutions:
files = [Path(x).absolute() for x in os.listdir(source_folder)]
or
files = [os.path.abspath(x) for x in os.listdir(source_folder)]
Result in appending current working dir to the correctly found names of the files. So, if I list directory: /other_home/data/gene_data
(which has a file named my_file.tsv
), but I am in some /new_home/def/cram/my_project_name
, The above solutions produce the path: /new_home/def/cram/my_project_name/my_file.tsv
and not /other_home/data/gene_data/my_file.tsv
. Is there a way still to achieve that?