Skip to content
Advertisement

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******Documentsfolderexample’

the problem is that it should be :

‘C:Users******DocumentsfolderTRACKERexample’

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?

Advertisement

Answer

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()
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement