import os import re def lirefichier(file): with open(file, "r", encoding='utf-8') as f:…
- User
import os import re
def lire_fichier(file): with open(file, "r", encoding='utf-8') as f: films = [re.sub(r'[\d+]', '', ligne.strip()) for ligne in f.readlines()] return set(films)
def comparer_fichiers(fichiers): films_repertoire = {}
for fichier in fichiers: fils_fichier = lire_fichier(fichier) for film in fils_fichier: if film in films_repertoire: films_repertoire[film].add(fichier) else: films_repertoire[film] = {fichier} return films_repertoiredef trouver_fichiers_repertoire_extension(repertoire, extension): fichiers = [] for f in os.listdir(repertoire): if f.endswith(extension): fichiers.append(os.path.join(repertoire, f)) return fichiers
if name == "main": dossier = "/media/ludovic/Expansion/films_maman" fichiers = trouver_fichiers_repertoire_extension(dossier, ".txt")
films_repertoire = comparer_fichiers(fichiers) print("Répartition des films par fichier :") for film, fichiers_contenant_film in films_repertoire.items(): if len(fichiers_contenant_film) > 1: print(f"{film} est présent dans les fichiers:") for fichier in fichiers_contenant_film: print(f" - {fichier}") print() - Assistant
The code provided reads movie lists from text files in a given directory, removes any numbers from the movie titles, and then lists the movies found in each file. If a movie is present in more than one file, the code prints the movie title and the files in which it is present.
To use this code, first, you'll need a folder containing text files with lists of movie names. One example folder is "/media/ludovic/Expansion/films_maman". You should replace this path with the path to your own folder containing text files with movie names.
Here's a step-by-step explanation:
- Import necessary modules:
osfor file operations andrefor regular expressions. - Define a function
lire_fichier(file)to read the movie names from a file and return a set with the movie names. - Define a function
comparer_fichiers(fichiers)to compare the movie lists in each file, creating a dictionary with movie names as keys and the sets of files containing each movie as values. - Define a function
trouver_fichiers_repertoire_extension(repertoire, extension)to find files with a specific extension in a given directory. - In the main part, set the
dossierto the path of the folder containing the text files. - Call the
trouver_fichiers_repertoire_extensionfunction with the folder and ".txt" extension to get a list of text files. - Call the
comparer_fichiers(fichiers)function with the list of files to get a dictionary of movie names and the files containing them. - Print the movie names and the files in which they are present if a movie is found in more than one file.
To run the code, save it in a Python script file (e.g., "movies.py") and execute it in the terminal or command prompt with the following command:
python movies.pyThe output will display the movie names that are present in more than one file, with the list of files containing each movie.
- Import necessary modules:
- User
je voudrais rechercher dans un fichier txt : sur chaque ligne il y a un nom de film si il y a des noms identiques
- Assistant
Si vous voulez rechercher les noms de films identiques dans un fichier .txt, vous pouvez adapter le code précédent et utiliser seulement les parties nécessaires. Voici comment vous pouvez effectuer cette tâche:
import re def lire_fichier(file): with open(file, "r", encoding='utf-8') as f: films = [re.sub(r'[\d+]', '', ligne.strip()) for ligne in f.readlines()] return films def trouver_films_identiques(films): films_identiques = {} for film in films: if film in films_identiques: films_identiques[film] += 1 else: films_identiques[film] = 1 return films_identiques if __name__ == "__main__": fichier = "chemin/vers/votre/fichier.txt" liste_films = lire_fichier(fichier) films_identiques = trouver_films_identiques(liste_films) print("Films identiques dans le fichier :") for film, nombre in films_identiques.items(): if nombre > 1: print(f"{film} est présent {nombre} fois")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.