Concatenation of All Possible Combinations of Files with Python
Question: I'll preface this by admitting I'm very new to Python. I have a directory of
files that I would like to see all the possible combinations. I've located a
script that can concatenate one set list of files but I would like to see all
the possible combinations of these files have them concatenated and save them
as a jpg. To make things more complicated I have two specific files that I
always want at the beginning of all combinations and one specific file that I
always want at the end.

Slapping some code I found from a similar question together I've come up with
the below which obviously isn't going to to cut it.

    
    
    files = ['File1.txt', 'Filet2.txt']
    
    with open('CombinedFile.jpg', 'w') as result:
            for file_ in files:
                for line in open(file_, 'r'):
                    result.write(line)
    

EDIT: I realized I may not have articulated the question well enough I would
like to combine the contents of the file per below:

Combine:

> File1.txt Contents
>
> File2.txt Contents
>
> File3.txt Contents

Output Result:

> CombinationFile.jpg

And then again with the files in different order until all possible
combinations have been run.


Answer: If you mean `permutation` saying `combination`:

    
    
    import itertools
    
    files = ['File1.txt', 'File2.txt', 'File3.txt']
    
    it = itertools.permutations(files, len(files))
    
    for x in it:
         print x
    

result

    
    
    ('File1.txt', 'File2.txt', 'File3.txt')
    ('File1.txt', 'Filt3.txt', 'File2.txt')
    ('File2.txt', 'File1.txt', 'File3.txt')
    ('File2.txt', 'File3.txt', 'File1.txt')
    ('File3.txt', 'File1.txt', 'File2.txt')
    ('File3.txt', 'File2.txt', 'File1.txt')
    

