1

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?

2 Answers 2

1

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()
Sign up to request clarification or add additional context in comments.

2 Comments

worked perfectly, but what's the point of file.close()?
It is used to close the iterator object obtained by os.scandir(). For more details check this link
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.