Python glob multiple filetypes

Is there a better way to use glob.glob in python to get a list of multiple file types such as .txt, .mdown, and .markdown? Right now I have something like this: projectFiles1 = glob.glob( os.path.join(projectDir, ‘*.txt’) ) projectFiles2 = glob.glob( os.path.join(projectDir, ‘*.mdown’) ) projectFiles3 = glob.glob( os.path.join(projectDir, ‘*.markdown’) ) 36 Answers 36 Maybe there is … Read more

How are glob.glob()’s return values ordered?

I have written the following Python code: #!/usr/bin/python # -*- coding: utf-8 -*- import os, glob path=”/home/my/path” for infile in glob.glob( os.path.join(path, ‘*.png’) ): print infile Now I get this: /home/my/path/output0352.png /home/my/path/output0005.png /home/my/path/output0137.png /home/my/path/output0202.png /home/my/path/output0023.png /home/my/path/output0048.png /home/my/path/output0069.png /home/my/path/output0246.png /home/my/path/output0071.png /home/my/path/output0402.png /home/my/path/output0230.png /home/my/path/output0182.png /home/my/path/output0121.png /home/my/path/output0104.png /home/my/path/output0219.png /home/my/path/output0226.png /home/my/path/output0215.png /home/my/path/output0266.png /home/my/path/output0347.png /home/my/path/output0295.png /home/my/path/output0131.png /home/my/path/output0208.png /home/my/path/output0194.png In which … Read more

How to loop over files in directory and change path and add suffix to filename

I need to write a script that starts my program with different arguments, but I’m new to Bash. I start my program with: ./MyProgram.exe Data/data1.txt [Logs/data1_Log.txt]. Here is the pseudocode for what I want to do: for each filename in /Data do for int i = 0, i = 3, i++ ./MyProgram.exe Data/filename.txt Logs/filename_Log{i}.txt end … Read more

How to use glob() to find files recursively?

This is what I have: glob(os.path.join(‘src’,’*.c’)) but I want to search the subfolders of src. Something like this would work: glob(os.path.join(‘src’,’*.c’)) glob(os.path.join(‘src’,’*’,’*.c’)) glob(os.path.join(‘src’,’*’,’*’,’*.c’)) glob(os.path.join(‘src’,’*’,’*’,’*’,’*.c’)) But this is obviously limited and clunky. 28 s 28 pathlib.Path.rglob Use pathlib.Path.rglob from the the pathlib module, which was introduced in Python 3.5. from pathlib import Path for path in … Read more