Shavow
Shavow

Reputation: 102

absolute path for file not working properly python

basically, I'm trying to store the full path for a file in a list but for some reason os.path.abspath() doesnt seem to work properly

files = os.listdir("TRACKER/")
for f in files:
    original_listpaths.append(os.path.abspath(f))

print(original_listpaths)

but my output seems to output this :

'C:\Users\******\Documents\folder\example'

the problem is that it should be :

'C:\Users\******\Documents\folder\TRACKER\example'

the difference is that the second one (the correct one) has the TRACKER included which is the official full path for that file but for some reason my output doesn't include the TRACKER and eliminates it, What's the problem?

Upvotes: 1

Views: 311

Answers (2)

Aejjee
Aejjee

Reputation: 189

You need to change your directory to "TRACKER" first. Just put os.chdir("TRACKER") before the loop starts after files = os.listdir("TRACKER/").

Upvotes: 0

Akhilraj Nambiar
Akhilraj Nambiar

Reputation: 306

You could try the following code:

files = os.scandir("TRACKER/")
print(files)
original_listpaths = []
for f in files:
    original_listpaths.append(os.path.abspath(f))

print(original_listpaths)
files.close()

Upvotes: 1

Related Questions