How do I read all files in a defferents folders in a main folder after storing them inside a directory so that I can perform some action on them?

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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′

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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

New contributor

Yassir AMEGAROU is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật