Reputation: 4352
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?
Upvotes: 0
Views: 240
Reputation: 77337
os.listdir
returns the filename without path, which makes it a relative path based on source_folder
. os.path.abspath
doesn't know about source_folder
. All it knows about is the current working directory. So, it assumes CWD is the relative path that you want to be made absolute.
You could join the known path component before making it absolute
files = [os.path.abspath(os.path.join(source_folder, x))
for x in os.listdir(source_folder)]
When using Path
you have a similar problem. You created the path from the filename which lacks its path component, so Path
assumes you meant the current working directory. Instead have Path
do the directory enumeration for you so that it passes back properly qualified Path
objects.
files = [x.absolute() for x in Path(source_folder).glob("*")]
Upvotes: 2