build array list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: build array list (/thread-7512.html) |
build array list - raopatwari - Jan-13-2018 I am writing function to return the list of files in unix directory. it is returning one file. fl = [] WORKING_DIR='../'+sys.argv[1]+'/' def find_case_insensitve(dirname, extensions): WORKING_DIR='../'+sys.argv[1]+'/' for filename in glob.glob(dirname): base, ext = os.path.splitext(filename) if ext.lower() in extensions: fl.append(filename) return list(fl) print(fl) fln=find_case_insensitve(WORKING_DIR+"*", ['.sql']) print(fln) RE: build array list - j.crater - Jan-13-2018 Post your code again in Python code tags, so it will be readable. You can find help here. RE: build array list - raopatwari - Jan-13-2018 fl = [] WORKING_DIR='../'+sys.argv[1]+'/' def find_case_insensitve(dirname, extensions): WORKING_DIR='../'+sys.argv[1]+'/' for filename in glob.glob(dirname): base, ext = os.path.splitext(filename) if ext.lower() in extensions: fl.append(filename) return list(fl) print(fl) fln=find_case_insensitve(WORKING_DIR+"*", ['.sql']) print(fln) RE: build array list - wavic - Jan-13-2018 Please, fix the indentation! RE: build array list - j.crater - Jan-13-2018 (Jan-13-2018, 08:34 PM)wavic Wrote: Please, fix the indentation! Quickest way to do this is to use ctrl+shift+v when copy-pasting code.
RE: build array list - Gribouillis - Jan-13-2018 Here is how I would do with pathlib from pathlib import Path def find_case_insensitive(dirname, extensions): sufset = set(x.lower() for x in extensions) return [f for f in Path(dirname).iterdir() if f.suffix.lower() in sufset] if __name__ == '__main__': working_dir = Path('..')/'2018-01' print(working_dir) print(find_case_insensitive(working_dir, ['.py', '.csv']))Actually, it would be more python3ish to return an iterable from find_case_insensitive() instead of a list. RE: build array list - raopatwari - Jan-14-2018 fl = [] fln = [] WORKING_DIR='../'+sys.argv[1]+'/' def find_case_insensitve(dirname, extensions): WORKING_DIR='../'+sys.argv[1]+'/' for filename in glob.glob(dirname): base, ext = os.path.splitext(filename) if ext.lower() in extensions: fl.append(filename) #print(filename) return list(fl)btw, I just started coding today in python. I guess I have to go long way I can print(filename) when I comment return list(fl) I want to use in the following way fln=find_case_insensitve(WORKING_DIR+"*", [".sql"]) print(fln) I will be using fln array in some other function RE: build array list - wavic - Jan-14-2018 If you want to use glob library there is no need to use os.path.splitext to get the extension: import glob import sys extensions = ('mp3', 'avi', 'mp4', 'mkv'] def find_media_files(path, files): result = [] for ext in files: pattern = path + '/*.' + ext # this will become /path/*.mp3 to search for. found = glob.glob(pattern) result.extend(found) return result my_collection = find_media_files(sys.argv[1], extensions) for file_ in my_collection: print(file_) RE: build array list - raopatwari - Jan-14-2018 Thank you |