Как прочитать все файлы внутри списка каталогов? [дубликат]Python

Программы на Python
Anonymous
Как прочитать все файлы внутри списка каталогов? [дубликат]

Сообщение Anonymous »

Я могу читать все файлы в трех разных папках, но не могу прочитать четвертую r'L:\Production\02\Database\Production_SVI\Data_Base\21040'.
Я могу открывать файлы в разных папках основной папки с помощью os.listdir, но как после этого прочитать каждый файл во всех каталогах?
Я попробовал это, и это работает, но я не могу прочитать все папки.
Какой объем данных мы можем прочитать и сохранить для обработки данных с помощью Python с использованием Pandas?

Код: Выделить всё

import os
import pandas as pd

directory_paths =[
r'L:\Production\02-Database\Production_SVI\Data_Base\21025',
r'L:\Production\02-Database\Production_SVI\Data_Base\23049',
r'L:\Production\02-Database\Production_SVI\Data_Base\23050',
r'L:\Production\02-Database\Production_SVI\Data_Base\21024',
r'L:\Production\02-Database\Production_SVI\Data_Base\21040'
]

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)

Как вы можете видеть в этом фрагменте вывода:
скрипт не может прочитать этот путь r'L:\Production\02-Database\ Production_SVI\Data_Base\21040'

Код: Выделить всё

Fichier CSV lu : L:\Production\02-Database\Production_SVI\Data_Base\21024\DB_21209_WUSU_NTM88_GEN2_434_PetD42_ALCAR_BLACK_ICT_21024.csv
Fichier CSV lu : L:\Production\02-Database\Production_SVI\Data_Base\21024\DB_21209_WUSU_NTM88_GEN2_434_PetD42_ALCAR_BLACK_ICT_21024_GOLDEN.csv
Fichier CSV lu : L:\Production\02-Database\Production_SVI\Data_Base\21024\DB_21210_WUSU_LAU_GEN2_434_PetD52_ALCAR_BLACK_ICT_21024.csv
Fichier CSV lu : L:\Production\02-Database\Production_SVI\Data_Base\21024\DB_21210_WUSU_LAU_GEN2_434_PetD52_ALCAR_BLACK_ICT_21024_GOLDEN.csv
Fichier CSV lu : L:\Production\02-Database\Production_SVI\Data_Base\21024\DB_21211_WUSU_LAU_GEN2_434_PetD62_ALCAR_BLACK_ICT_21024.csv
Fichier CSV lu : L:\Production\02-Database\Production_SVI\Data_Base\21024\DB_21211_WUSU_LAU_GEN2_434_PetD62_ALCAR_BLACK_ICT_21024_GOLDEN.csv
Fichier CSV lu : L:\Production\02-Database\Production_SVI\Data_Base\21024\DB_23078_WUS_NTM88_AUTO_BLE_ALCAR_TESLA_DARK_GREY_ICT_21024.csv
Fichier CSV lu : L:\Production\02-Database\Production_SVI\Data_Base\21024\DB_23078_WUS_NTM88_AUTO_BLE_ALCAR_TESLA_DARK_GREY_ICT_21024_GOLDEN.csv
Traitement des dossiers dans le répertoire: L:\Production\02-Database\Production_SVI\Data_Base\21040
Вывод кода:
Изображение


Подробнее здесь: https://stackoverflow.com/questions/784 ... irectories

Вернуться в «Python»