Preksha Sharma
Preksha Sharma

Reputation: 11

Compare directory content to a text file

I am trying to compare the contents of a directory to a text file.I have certain files in the directory and I also want to compare the files to this text file. How do I achieve this?

Upvotes: 0

Views: 57

Answers (1)

nessuno
nessuno

Reputation: 118

To get the directory's content (list of filenames):

import os
dir_content = os.listdir(directory_path)

To get a text-files content (line by line in a list):

with open(filename) as f: 
    lines = f.readlines()

Upvotes: 1

Related Questions