Please review and comment/correct my error! Many thanks in advance.
# Required module import os # Function for getting files from a folder def fetchFiles(pathToFolder, flag, keyWord): ''' fetchFiles() requires three arguments: pathToFolder, flag and keyWord flag must be 'STARTS_WITH' or 'ENDS_WITH' keyWord is a string to search the file's name Be careful, the keyWord is case sensitive and must be exact. Example: fetchFiles('/Documents/Photos/','ENDS_WITH','.jpg') returns: _pathToFiles and _fileNames ''' _pathToFiles = [] _fileNames = [] for dirPath, dirNames, fileNames in os.walk(pathToFolder): if flag == 'ENDS_WITH': selectedPath = [os.path.join(dirPath,item) for item in fileNames if item.endswith(keyWord)] _pathToFiles.extend(selectedPath) selectedFile = [item for item in fileNames if item.endswith(keyWord)] _fileNames.extend(selectedFile) elif flag == 'STARTS_WITH': selectedPath = [os.path.join(dirPath,item) for item in fileNames if item.startswith(keyWord)] _pathToFiles.extend(selectedPath) selectedFile = [item for item in fileNames if item.startswith(keyWord)] _fileNames.extend(selectedFile) else: print fetchFiles.__doc__ break # Try to remove empty entries if none of the required files are in directory try: _pathToFiles.remove('') _imageFiles.remove('') except ValueError: pass # Warn if nothing was found in the given path #if selectedFile == []: #print 'No files with given parameters were found in:\n', dirPath, '\n' #print len(_fileNames), 'files were found is searched folder(s)' #return _pathToFiles, _fileNames #print _pathToFiles, _fileNames #print _pathToFiles [1] print _pathToFiles s = ' '.join(_pathToFiles [1]) #convert tuple element 1 to string #print len (s) s_no_white = s.replace(" ", "") #remove white spaces #print s_no_white[4:7] #extract rgeistration s1 = s_no_white[4:7] #extract rgeistration print 'str1 is:', str1 str2 = 'FLDAT' print 'str2 is: ', str2 str3 = str1.__add__(str2) print 'str 3 is: ',str3 str4 = 'H//' print 'str4 is: ', str4 str5 = str4.__add__(str3) print 'str5 is: ', str5 print _fileNames print 'Number of files found: ', len(_fileNames) fetchFiles('str5','ENDS_WITH','.FLD') ####code end## ###Output## File "D:/university_2/my_code_2/good_2016_12_01/get_registration.py", line 52, in fetchFiles s = ' '.join(_pathToFiles [1]) #convert tuple element 1 to string IndexError: list index out of range ##output end## _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor