0

I was making a function that recursively searches directories for files with a particular suffixes.

TypeError: slice indices must be integers or None or have an index method

pointing to this line:

if path.endswith('.',sf)==True: l.append(path)

.endswith() returns a boolean and is used to test Strings, why the hell is it giving me issues about non-integers?

also I decided to just print everything and throw in a try/except statement if a file is not a directory or a file(cause that happend pretty quickly into the first run). it ran fine for a minute or two then started spitting out the except clause

something went wrong with /sys/bus/cpu/devices/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu1/node0/cpu0/node0/cpu1/subsystem/devices/cpu0/node0/cpu0/subsystem/devices/cpu0/subsystem/devices/cpu0/subsystem something went wrong with /sys/bus/cpu/devices/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu1/node0/cpu0/node0/cpu1/subsystem/devices/cpu0/node0/cpu0/subsystem/devices/cpu0/subsystem/devices/cpu0 something went wrong with /sys/bus/cpu/devices/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu0/node0/cpu1/node0/cpu0/node0/cpu1/subsystem/devices/cpu0/node0/cpu0/subsystem/devices/cpu0/subsystem/devices/cpu1/cache/power

well I just Ctrl-Z’d went back into ipython3 and tried again, which immediately brought up the same message, even though the specified directory was / . why did it start back at this same area?

edit: code

def recursive_search(dr, sf):
  """searches every potential path from parent directory for files     with     the matching suffix"""
  l=[]#for all the good little scripts and files with the right last name
  for name in os.listdir(dr):#for every item in directory
 path=os.path.join(dr, name)#path is now path/to/item

 if os.path.isfile(path):
   if path.endswith('.',sf)==True:
     l.append(path)
 else:
   #try:
   recursive_search(path, sf)
   #except:
     #print ('something went wrong with ', path)

if it comes out looking weird I'm having some trouble with the formatting.

Pablo
  • 4,821
  • 12
  • 52
  • 82
user58641
  • 3
  • 2

1 Answers1

0

Take a look at the documentation for str.endswith:

>>> help(str.endswith)
Help on method_descriptor:

endswith(...)
    S.endswith(suffix[, start[, end]]) -> bool

    Return True if S ends with the specified suffix, False otherwise.
    With optional start, test S beginning at that position.
    With optional end, stop comparing S at that position.
    suffix can also be a tuple of strings to try.

So calling "path".endswith('.',"py") checks the string path if it ends with "." starting it's check from the indice "py", however "py" is not a valid indice, hence the error.

To check if the string ends with ".py" you need to add the strings together instead of passing it as another argument:

path.endswith("."+sf)
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59