(Aug-20-2021, 08:12 AM)AlphaInc Wrote: Could it be because the files got spaces in it?I couple of tips how you troubleshoot this.
def sort_by_int(path): print(path) print(path.stem) # Path has the stem attribute, which isBye adding this you see what happen before error.
Test.
>>> f = Path(r'G:\div_code\answer\file_33.txt') >>> f.stem 'file_33' >>> f.stem.split('_', maxsplit=1) ['file', '33'] >>> f.stem.split('_', maxsplit=1)[1] '33'Make your error.
>>> f = Path(r'G:\div_code\answer\file33.txt') >>> f.stem 'file33' >>> f.stem.split('_', maxsplit=1) ['file33'] >>> f.stem.split('_', maxsplit=1)[1] Traceback (most recent call last): File "<interactive input>", line 1, in <module> IndexError: list index out of rangeIn your first post all files you show file_1,file_2,file_3,file_10...ect all had a
_
,then it should work.With those
print()
or add repr()
(see all like eg space) you will se all files input before the error. print(repr(path)) print(repr(path.stem))