0

I have the below files located at some location in RHEL machine. temp_file2.txt temp_file3.txt

Looking for a python script to find above files recursively in all directories(I used a wild card, but it didn't work), and print a message if the file exists or not.

The below code snippet returns Nothing

import glob

for filename in glob.iglob('*/*.txt', recursive=True):
    print(filename)

It returns the file name if it exists only in the current working directory

import glob

for filename in glob.iglob('.txt', recursive=True):
    print(filename)
2
  • I think you need **, so glob.iglob('**/*.txt', recursive=True): Commented Nov 11, 2018 at 2:34
  • I have tried ** also, and I noticed it returns files in the current working directory only. Commented Nov 11, 2018 at 3:03

1 Answer 1

1

This approach seems to have worked for me, using python3.6

import glob

for f in glob.iglob('./**/*.yml', recursive=True):
    print(f)

I was also able to use os.getcwd() + '/**/*.yml'. It appears there must be a directory definition at the start of the glob.

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

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.