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 906125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:20:04+00:00 2026-05-15T16:20:04+00:00

so I have a python script which takes the filename as a command argument

  • 0

so I have a python script which takes the filename as a command argument and processes that file. However, because I have 263 files which need the same processing, I was wondering whether the command argument section could be modified with a for loop to consecutively run through all the files in a folder? Cheers, Sat

EDIT:

The code for the system argument is here:

try:
    opt_list, args = getopt.getopt(sys.argv[1:], 'r:vo:A:Cp:U:eM:')

except getopt.GetoptError, msg:
    print 'prepare_receptor4.py: %s' %msg
    usage()
    sys.exit(2)

with ‘r’ being the name of the file needing to be processed and the others are optional arguments. I’m not sure how to modify this with a for loop.

  • 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-05-15T16:20:05+00:00Added an answer on May 15, 2026 at 4:20 pm

    As a practical matter, whatever shell you’re using probably has some syntax that can be easily used for this. In Bash, for example:

    for f in *; do python myscript.py $f; done
    

    To actually do this in Python, I’d suggest structuring your program so that the main code is in a function which takes one argument, the filename.

    def process(filename):
        ...code goes here...
    

    Then you can invoke this function like so,

    for f in os.listdir(folder):
        process(f)
    

    folder could be passed as a command-line argument, or just written into the script (if it’s not something you’d be reusing).

    EDIT: In response to your edit, I’d suggest just giving the filenames as regular command-line arguments, without using the -r option, so that they’ll wind up in args. Then you can do

    for f in args:
        process(f)
    

    or if you would rather pass the directory name as the command-line argument,

    for d in args:
        for f in os.listdir(d):
            process(f)
    

    Alternatively, I suppose you could pass multiple instances of the -r option, and then do

    for opt, arg in opt_list:
        if opt == '-r':
            process(arg)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python script that runs a program, which generates few .exe files
I have the following python script which takes some inputs and puts them in
I have a Python3 script that can take optional command line parameters which the
I have a script that records files with UTF8 encoded names. However the script's
So I have a python script that runs a loop in which it calls
I have a Python script which takes a long time to run. I'd quite
I have a PYTHON CGI script(Content-type: text/plain ) which takes about 10 minutes to
I'm writing a Python script that takes in a (potentially large) file. Here is
I have a task of creating a script which takes a huge text file
Basically I have a script in Python that takes several letters, gets every combination

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.