Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· OpenAIAi2 WildChat-4.8M (AI2)

import os import re def lirefichier(file): with open(file, "r", encoding='utf-8') as f:…

  1. 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_repertoire
    

    def 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()
    
  2. 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:

    1. Import necessary modules: os for file operations and re for regular expressions.
    2. Define a function lire_fichier(file) to read the movie names from a file and return a set with the movie names.
    3. 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.
    4. Define a function trouver_fichiers_repertoire_extension(repertoire, extension) to find files with a specific extension in a given directory.
    5. In the main part, set the dossier to the path of the folder containing the text files.
    6. Call the trouver_fichiers_repertoire_extension function with the folder and ".txt" extension to get a list of text files.
    7. Call the comparer_fichiers(fichiers) function with the list of files to get a dictionary of movie names and the files containing them.
    8. 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.py
    

    The output will display the movie names that are present in more than one file, with the list of files containing each movie.

  3. 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

  4. 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")
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.