0
for root, subdirs, files in os.walk(dir): 
    for i in range(0, files.index(files[-1]) + 1):

files.index(files[-1]) here is the end index of item in files, and I got it.

But I want to know is there a simple way to get it? I know if I got the end item, say, like "d", after typing in files.index("d"), I would have gotten the index.

Alperen
  • 3,772
  • 3
  • 27
  • 49

2 Answers2

2

files is a list, then you can use len() function. So, files.index(files[-1]) is equal to len(files) - 1.

Actually your question is about finding the last index. Take a look at this link.

Alperen
  • 3,772
  • 3
  • 27
  • 49
0

You can find the last letter (starting from the last letter):

s = 'acbc'
print(s.find(s[-1], -1))

gives you 3, which is the index of the last letter. The same you can use for your case.

mrCarnivore
  • 4,638
  • 2
  • 12
  • 29