Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8360077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:21:03+00:00 2026-06-09T11:21:03+00:00

I managed to modify sample code I found on the internet to find all

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:21:05+00:00Added an answer on June 9, 2026 at 11:21 am
    x=3
    
    for combination in itertools.combinations(files, x):
      counter = counter + 1
      output = " and ".join(combination)
      print output, counter
    

    command line arguments can be fetched with sys.argv

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create an application capable to modify and manage files on IOS.
I managed to create a dynamic three dropdown list using Javascript code and it
I managed to apply this excellent example to my needs: http://code.google.com/p/galleriffic/issues/attachmentText?id=76&aid=-3045121742886940548&name=example-2.html&token=4nlvmgAfVpc5whcXC9UEqHZaSz0%3A1339257750721 With a small
I managed to request the publish_actions permission, but since it is revocable from the
I managed to found out how to delete field but I have a problem
I managed to get some help from a previous question and I have the
I'm developing a java servlet web application that manages information from multiple databases (all
Can someone give me a sample code to confine the cursor on to a
Has anyone managed to modify the default font size for a AutoComplete TextBox, Windows
How can I stream data (text) from a managed assembly to a native library

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.