![]() ![]() We can do this with argparse or sys.argv. To make the script easier to use and test, I'd recommend reading in the target (top-level) directory and the filename extension as command-line arguments. ![]() Calling os.path.abspath on the input directory path ensures this. For each 3-tuple (root, dirs, files), root is the containing directory and files is a list of non-directory files that reside directly under root.ĭo note that if we want each root directory (as mentioned above) to be an absolute path, we need to pass in an absolute path to os.walk. That is, instead of building a List]]], build a Dict].Ī single call to os.walk is sufficient to perform the job that yield_files_in_subfolders and yield_files_with_extensions are currently doing together. files=" + str(len(a)))Ī dictionary is the more appropriate data structure to use here, since your code is essentially building a mapping from directory paths to lists of matched filenames. If fnmatch.fnmatch(file.lower(),file_match.lower()):īreak # without this line it traverses the subfolders tooĭef yield_files_in_subfolders(folder_path, file_match): import osĭef yield_files_with_extensions(folder_path, file_match):įor root, dirs, files in os.walk(folder_path): So, I thought, why not give try Python a try ? The web page will eventually use javascript on the client-side for the user to choose a file from a list and "cast" it from the Pi4's apache2 web server over to the chromecast device. I hope to use the code to generate a nightly json file underpinning a chromecasting web server on a Raspberry Pi 4, based on Google's example chrome (web page) "sender". The python3 script below fetches a large list of matching files in a folder tree, in the format: ]īeing only a python newbie, there HAS to be a better way ? ![]() Still unsure of the python terminology at the moment :( I am an old python newbie here, migrating from fortran77 ) ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |