Skip to content
Advertisement

How do I output a recursive list of files to a text file without using glob (python3.4)

I am trying to recursively go through all the directories in the “boards” directory and find files that end in ‘.vhd’ and then output them to a text file. I am using python 3.4 so I don’t have access to recursive glob.

path = '../../boards'

rel_paths = open('rel_paths.txt', 'a+')

files = [os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(path)
    for f in files
        if f.endswith('.vhd')]

I want ‘rel_paths.txt’ to look like this in the inside:

../../boards/foo/bar/file_name1.vhd
../../boards/foo/bars/file_name2.vhd

Advertisement

Answer

if you are flexible you can use Unix command ” find ” instead of writing python code as follows

find ../../boards -name "*.vhd" >>  rel_paths.txt

it can be modified to suit what you need

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement