I am able to open the files in the defferents folders in the main folder using os.listdir, but after that how do I read every file and then execute some code on them ?
i did try this and it works but i can’t read all folders.
how much data could we read and store to precess data with python with the use of Pandas.
Thank you
r'L:Production2-DatabaseProduction_SVIData_Base21025',
r'L:Production2-DatabaseProduction_SVIData_Base23049',
r'L:Production2-DatabaseProduction_SVIData_Base23050',
r'L:Production2-DatabaseProduction_SVIData_Base21024',
r'L:Production2-DatabaseProduction_SVIData_Base21040'
def extract_details(file_path):
parts = file_path.split("\")
filename_part = parts[-1]
filename_parts = filename_part.split('_')
# Vérification de la présence de "Golden" comme dernier élément du nom de fichier
if "GOLDEN" in filename_parts[-1]:
ref_produit = filename_parts[-2] # Prendre l'élément avant "Golden"
ref_produit = filename_parts[-1].split('.')[0] # Sinon prendre le dernier élément et retirer l'extension
#type_produit = filename_parts[-2] # L'élément avant le dernier est 'Type.Produit'
ict_fct = "ICT" if "ICT" in filename_part else ("FCT" if "FCT" in filename_part else None) #detcter si c'est un fichier ICT ou FCT
is_golden = 1 if "GOLDEN" in filename_part.upper() else 0 #detecter si le fichier est golden ou pas
return baie, ref_produit, ict_fct, is_golden #type_produit
def extract_site_prod(file_path):
if 'INOVELEC' in file_path:
return 'Site Inconnu' # Si le site n'est pas reconnu
def read_all_csv_in_directory(directory_paths):
for path in directory_paths:
print(f"Traitement des dossiers dans le répertoire: {path}")
site_prod = extract_site_prod(path)
for root, dirs, files in os.walk(path):
if file.endswith('.csv'):
file_path = os.path.join(root, file)
baie, ref_produit, ict_fct, is_golden = extract_details(file_path)
df = pd.read_csv(file_path, sep='|')
df['RefProduit'] = ref_produit
#df['TypeProduit'] = type_produit
df['Site_Prod'] = site_prod
dataframes_list.append(df)
print(f"Fichier CSV lu : {file_path}")
except pd.errors.EmptyDataError:
print(f"Aucune donnée à lire dans le fichier {file_path}.")
except pd.errors.ParserError as e:
print(f"Erreur de parse lors de la lecture du fichier {file_path}: {e}")
print(f"Erreur lors de la lecture du fichier {file_path}: {e}")
combined_df3 = pd.concat(dataframes_list, ignore_index=True)
print("Aucun DataFrame à concaténer.")
combined_df3 = read_all_csv_in_directory(directory_paths)
<code>import os
import pandas as pd
directory_paths =[
r'L:Production2-DatabaseProduction_SVIData_Base21025',
r'L:Production2-DatabaseProduction_SVIData_Base23049',
r'L:Production2-DatabaseProduction_SVIData_Base23050',
r'L:Production2-DatabaseProduction_SVIData_Base21024',
r'L:Production2-DatabaseProduction_SVIData_Base21040'
]
def extract_details(file_path):
parts = file_path.split("\")
filename_part = parts[-1]
filename_parts = filename_part.split('_')
baie = filename_parts[1]
# Vérification de la présence de "Golden" comme dernier élément du nom de fichier
if "GOLDEN" in filename_parts[-1]:
ref_produit = filename_parts[-2] # Prendre l'élément avant "Golden"
else:
ref_produit = filename_parts[-1].split('.')[0] # Sinon prendre le dernier élément et retirer l'extension
#type_produit = filename_parts[-2] # L'élément avant le dernier est 'Type.Produit'
ict_fct = "ICT" if "ICT" in filename_part else ("FCT" if "FCT" in filename_part else None) #detcter si c'est un fichier ICT ou FCT
is_golden = 1 if "GOLDEN" in filename_part.upper() else 0 #detecter si le fichier est golden ou pas
return baie, ref_produit, ict_fct, is_golden #type_produit
def extract_site_prod(file_path):
if 'INOVELEC' in file_path:
return 'INOVELEC'
elif 'LDL' in file_path:
return 'LDL'
elif 'SVI' in file_path:
return 'SVI'
else:
return 'Site Inconnu' # Si le site n'est pas reconnu
def read_all_csv_in_directory(directory_paths):
dataframes_list = []
for path in directory_paths:
print(f"Traitement des dossiers dans le répertoire: {path}")
site_prod = extract_site_prod(path)
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith('.csv'):
file_path = os.path.join(root, file)
baie, ref_produit, ict_fct, is_golden = extract_details(file_path)
try:
df = pd.read_csv(file_path, sep='|')
if not df.empty:
df['Baie'] = baie
df['RefProduit'] = ref_produit
#df['TypeProduit'] = type_produit
df['ICT_FCT'] = ict_fct
df['GOLDEN'] = is_golden
df['Site_Prod'] = site_prod
dataframes_list.append(df)
print(f"Fichier CSV lu : {file_path}")
except pd.errors.EmptyDataError:
print(f"Aucune donnée à lire dans le fichier {file_path}.")
except pd.errors.ParserError as e:
print(f"Erreur de parse lors de la lecture du fichier {file_path}: {e}")
except Exception as e:
print(f"Erreur lors de la lecture du fichier {file_path}: {e}")
if dataframes_list:
combined_df3 = pd.concat(dataframes_list, ignore_index=True)
return combined_df3
else:
print("Aucun DataFrame à concaténer.")
return pd.DataFrame()
combined_df3 = read_all_csv_in_directory(directory_paths)
</code>
import os
import pandas as pd
directory_paths =[
r'L:Production2-DatabaseProduction_SVIData_Base21025',
r'L:Production2-DatabaseProduction_SVIData_Base23049',
r'L:Production2-DatabaseProduction_SVIData_Base23050',
r'L:Production2-DatabaseProduction_SVIData_Base21024',
r'L:Production2-DatabaseProduction_SVIData_Base21040'
]
def extract_details(file_path):
parts = file_path.split("\")
filename_part = parts[-1]
filename_parts = filename_part.split('_')
baie = filename_parts[1]
# Vérification de la présence de "Golden" comme dernier élément du nom de fichier
if "GOLDEN" in filename_parts[-1]:
ref_produit = filename_parts[-2] # Prendre l'élément avant "Golden"
else:
ref_produit = filename_parts[-1].split('.')[0] # Sinon prendre le dernier élément et retirer l'extension
#type_produit = filename_parts[-2] # L'élément avant le dernier est 'Type.Produit'
ict_fct = "ICT" if "ICT" in filename_part else ("FCT" if "FCT" in filename_part else None) #detcter si c'est un fichier ICT ou FCT
is_golden = 1 if "GOLDEN" in filename_part.upper() else 0 #detecter si le fichier est golden ou pas
return baie, ref_produit, ict_fct, is_golden #type_produit
def extract_site_prod(file_path):
if 'INOVELEC' in file_path:
return 'INOVELEC'
elif 'LDL' in file_path:
return 'LDL'
elif 'SVI' in file_path:
return 'SVI'
else:
return 'Site Inconnu' # Si le site n'est pas reconnu
def read_all_csv_in_directory(directory_paths):
dataframes_list = []
for path in directory_paths:
print(f"Traitement des dossiers dans le répertoire: {path}")
site_prod = extract_site_prod(path)
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith('.csv'):
file_path = os.path.join(root, file)
baie, ref_produit, ict_fct, is_golden = extract_details(file_path)
try:
df = pd.read_csv(file_path, sep='|')
if not df.empty:
df['Baie'] = baie
df['RefProduit'] = ref_produit
#df['TypeProduit'] = type_produit
df['ICT_FCT'] = ict_fct
df['GOLDEN'] = is_golden
df['Site_Prod'] = site_prod
dataframes_list.append(df)
print(f"Fichier CSV lu : {file_path}")
except pd.errors.EmptyDataError:
print(f"Aucune donnée à lire dans le fichier {file_path}.")
except pd.errors.ParserError as e:
print(f"Erreur de parse lors de la lecture du fichier {file_path}: {e}")
except Exception as e:
print(f"Erreur lors de la lecture du fichier {file_path}: {e}")
if dataframes_list:
combined_df3 = pd.concat(dataframes_list, ignore_index=True)
return combined_df3
else:
print("Aucun DataFrame à concaténer.")
return pd.DataFrame()
combined_df3 = read_all_csv_in_directory(directory_paths)
Like you can see in this extract output:
the script can’t read this path r’L:Production2-DatabaseProduction_SVIData_Base21040′
<code>Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21209_WUSU_NTM88_GEN2_434_PetD42_ALCAR_BLACK_ICT_21024.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21209_WUSU_NTM88_GEN2_434_PetD42_ALCAR_BLACK_ICT_21024_GOLDEN.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21210_WUSU_LAU_GEN2_434_PetD52_ALCAR_BLACK_ICT_21024.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21210_WUSU_LAU_GEN2_434_PetD52_ALCAR_BLACK_ICT_21024_GOLDEN.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21211_WUSU_LAU_GEN2_434_PetD62_ALCAR_BLACK_ICT_21024.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21211_WUSU_LAU_GEN2_434_PetD62_ALCAR_BLACK_ICT_21024_GOLDEN.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_23078_WUS_NTM88_AUTO_BLE_ALCAR_TESLA_DARK_GREY_ICT_21024.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_23078_WUS_NTM88_AUTO_BLE_ALCAR_TESLA_DARK_GREY_ICT_21024_GOLDEN.csv
Traitement des dossiers dans le répertoire: L:Production2-DatabaseProduction_SVIData_Base21040
<code>Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21209_WUSU_NTM88_GEN2_434_PetD42_ALCAR_BLACK_ICT_21024.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21209_WUSU_NTM88_GEN2_434_PetD42_ALCAR_BLACK_ICT_21024_GOLDEN.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21210_WUSU_LAU_GEN2_434_PetD52_ALCAR_BLACK_ICT_21024.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21210_WUSU_LAU_GEN2_434_PetD52_ALCAR_BLACK_ICT_21024_GOLDEN.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21211_WUSU_LAU_GEN2_434_PetD62_ALCAR_BLACK_ICT_21024.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21211_WUSU_LAU_GEN2_434_PetD62_ALCAR_BLACK_ICT_21024_GOLDEN.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_23078_WUS_NTM88_AUTO_BLE_ALCAR_TESLA_DARK_GREY_ICT_21024.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_23078_WUS_NTM88_AUTO_BLE_ALCAR_TESLA_DARK_GREY_ICT_21024_GOLDEN.csv
Traitement des dossiers dans le répertoire: L:Production2-DatabaseProduction_SVIData_Base21040
</code>
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21209_WUSU_NTM88_GEN2_434_PetD42_ALCAR_BLACK_ICT_21024.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21209_WUSU_NTM88_GEN2_434_PetD42_ALCAR_BLACK_ICT_21024_GOLDEN.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21210_WUSU_LAU_GEN2_434_PetD52_ALCAR_BLACK_ICT_21024.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21210_WUSU_LAU_GEN2_434_PetD52_ALCAR_BLACK_ICT_21024_GOLDEN.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21211_WUSU_LAU_GEN2_434_PetD62_ALCAR_BLACK_ICT_21024.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_21211_WUSU_LAU_GEN2_434_PetD62_ALCAR_BLACK_ICT_21024_GOLDEN.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_23078_WUS_NTM88_AUTO_BLE_ALCAR_TESLA_DARK_GREY_ICT_21024.csv
Fichier CSV lu : L:Production2-DatabaseProduction_SVIData_Base21024DB_23078_WUS_NTM88_AUTO_BLE_ALCAR_TESLA_DARK_GREY_ICT_21024_GOLDEN.csv
Traitement des dossiers dans le répertoire: L:Production2-DatabaseProduction_SVIData_Base21040
enter image description here