1

I am developing a Python script in which I want to scan whole sub directories to find .log files.

To get a list of .log files, I can get the file names. Here is the code.

for root, dirs, files in os.walk("Directory to be analyse"):
    for file in files:
        if file.endswith('.log'):

Now, How to iterate all files in for loop and get the contents of it?

2
  • Is this a "How can I open and read a file in Python" question? Commented Feb 12, 2016 at 7:23
  • Yes. But Would like to open multiple files to parse it. There is not any fixed number of files Commented Feb 12, 2016 at 8:06

3 Answers 3

2

Try;

import os
for root, dirs, files in os.walk(r'path to open'):
    for file in files:
        if file.endswith('.log'):
            with open(os.path.join(root, file)) as stream:
                for lin in stream:
                    # lin containg the content of each line in the file

You can use os.path.join get the full path of the file to open

Sign up to request clarification or add additional context in comments.

Comments

0

In your if block, you can open the file in a with open block and read each line.

if file.endswith('.log'):
    with open(file,'r') as f:
        for line in f:
            pass  #Do something with each line in the opened file
    print '{} has been closed'.format(file)

Hope this helps!

Comments

0

If you are ok to do this with bash, then a one liner will do the job

DIR='Directory to be analyse'    
sudo find $DIR -iname \*.log -exec cat {} \;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.