0

I have a directory with lot of text files. I want to read each text file in the directory and perform some kind of search operation. I take directory name as a command line argument. The error I'm getting is IsADirectoryError. Is there anyway we can make this work without any other module?

This is my code:

a = sys.argv
files = a[1:-1]

for i in files:
    print(i)
    f = open(i,'rb')
    for line in f:
        try:
            for word in line.split():
            '''Rest of code here'''
coder7777
  • 378
  • 1
  • 5
  • 16

1 Answers1

0

try this code

def read_files_from_dir(dirname):
    for _file in os.listdir(dirname):
        with open(os.path.join(dirname,_file), "r") as fp:
            print fp.read()