(Feb-10-2020, 04:51 PM)jim2007 Wrote: So I did the same, but it is a function that returns a list of files"
Can't appreciate your time enough Jim, this lists all the files in my folder and I'm able to somewhat understand it! (except the fact that you use base_folder in the function but define it later on, I thought these codes were interpreted line by line) but that's something that I can research myself.
Now I'll go ahead and try to export these files that we printed into a main folder. I'll resurrect the thread if I get stuck again. :)
Thanks a bunch!
Edit: it didn't take long for me to come up with another question. Here is my final script;
import os, zipfile, shutil from typing import List FileList = List[str] def get_list_of_files(base_folder: str, files: FileList) -> None: with os.scandir(base_folder) as entries: for entry in entries: # Skip all hidden files and folders if entry.name.startswith('.'): continue qualified_name = os.path.join(base_folder, entry) # If it's a file we'll added it to the collection if entry.is_file(): files.append(qualified_name) elif entry.is_dir(): # If it's a folder then we'll process that as well get_list_of_files(os.path.join(base_folder, entry), files) files = [] base_folder = r"C:\Users\username\My_Dataset" get_list_of_files(base_folder, files) for file in files: print(file) my_zipfile = zipfile.ZipFile(file) my_zipfile.extractall(r'C:\Users\username\My_Dataset\new') # Generate the file paths to traverse, or a single path if a file name was given def getfiles(path): if os.path.isdir(path): for root, dirs, files in os.walk(path): for name in files: yield os.path.join(root, name) else: yield path destination = r"C:\Users\Documents\flatten" fromdir = r"C:\Users\username\My_Dataset\new" for f in getfiles(fromdir): filename = str.split(f, '/')[-1] if os.path.isfile(destination + filename): filename = f.replace(fromdir, "", 1).replace("/", "_") # os.rename(f, destination+filename) shutil.copy2(f, r"C:\Users\username\Documents\flatten")Here is my question; what if I only want to process files that have specific extensions and leave the rest? I imagine I should build an if else function but I'm not sure exactly where to put it and how to construct it.
For example
if filetype is = shp, shx, cpg, prj, dbf continueWould you have any input about this? Thanks a lot again in advance!