I managed to modify sample code I found on the internet to find all possible combinations of a group of files in pairs of 2 from within a folder.
If I have a folder test which contains the following files: file1, file2, file3, file4 and run the following code:
import os, itertools, glob
folder = "test"
files = glob.glob(folder + "/*")
counter = 0
for file1, file2 in itertools.combinations(files, 2):
counter = counter + 1
output = file1 + " and " + file2
print output, counter
My output is this:
test/file1 and test/file2 1
test/file1 and test/file3 2
test/file1 and test/file4 3
test/file2 and test/file3 4
test/file2 and test/file4 5
test/file3 and test/file4 6
Which is perfect for listing all of the possible groups of 2 files without repeating. Now, since I have my for loop hardcoded, I’m having issues scaling this to groups of “x” files and yet keep the code simple. IE, I would like “x” to be user-chosen such that if he chose 3, the script would show an output of:
test/file1 and test/file2 and test/file3 1
test/file1 and test/file2 and test/file4 2
test/file1 and test/file3 and test/file4 3
test/file2 and test/file3 and test/file4 4
The whole idea isn’t to actually show the output on stdout, but to use them as arguments in a subprocess call.
Any suggestions?
command line arguments can be fetched with
sys.argv