I have the following structure:
root
|_ Audi
|_file1.txt
|_file2.txt
|_ Mercedes
|_file1.txt
|_file2.txt
I want to create a function that checks wether a file is in Audi
or Mercedes
.
Mercedes
process..This is my code:
root = r'C:\data\desktop\my_folder\my_cars'
def move_to_db (path):
ls_mts_raw = []
for file in os.listdir(path):
if file.endswith('.csv'):
file_path = os.path.join(path, file)
move_to_db(root)
This is what I have untill now but I got stuck.....
With the following code, you will obtain a dict with the brands as keys and a list of file names as values.
import glob
from pathlib import Path
files = glob.glob(r"./root/*/*.txt") # You can use os.path.join() here
files_by_brand = {}
for file in files:
path = Path(file)
brand_name = path.parent.name
file_name = path.resolve().as_posix()
file_list = files_by_brand.setdefault(brand_name, [])
file_list.append(file_name)
print(files_by_brand)