Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • bio_informatics
    Senior Member
    • Nov 2013
    • 182

    getopts in python

    I am new to python, have been working in perl. I am trying to implement getopts in python, but I am failing to understand it.


    I have the below code:
    Code:
    #!/usr/bin/python
    
    from Bio import SeqIO
    import sys, getopt
    
    def main(argv):
       
       for i in argv:
        print "This is in for loop "+i
       
       listfile = ''
       pdbfile = ''
       
       try:
          opts, args = getopt.getopt(argv,"pdb:list:",["pfile=","lfile="])
       except getopt.GetoptError:
          print 'check.py -pdb <pdbfile> -list <listfile>'
          
          sys.exit(2)
       for opt, arg in opts:
          
          if opt in ("-pdb","--pfile"):
             pdbfile = arg
             
          if opt in ("-list","--lfile"):
             listfile = arg
       print 'Input file is "', listfile
       print 'Output file is "', pdbfile
    
    if __name__ == "__main__":
       main(sys.argv[1:])
    I run it as:
    ./check.py -pdb ~/Documents/pdb/pdb_seqres.txt -list ~/Documents/pdb/list.txt
    The output I get is:
    This is in for loop -pdb
    This is in for loop /home/sariya/Documents/pdb/pdb_seqres.txt
    This is in for loop -list
    This is in for loop /home/sariya/Documents/pdb/list.txt
    Input file is "
    Output file is "
    when I print arg, I get blank. so it tells me there is not value passed in arg. Why?

    So my pdbfile and listfile are getting blank.
    Please guide.
    Bioinformaticscally calm
  • blancha
    Senior Member
    • May 2013
    • 367

    #2
    You may have your reasons to prefer using the getopts module, but I really like the argparse module, especially in Python 3.4.

    Very simple, intuitive and powerful.

    Comment

    • R.W.W.Brouwer
      Junior Member
      • Mar 2010
      • 1

      #3
      In the Python getopts module, short arguments are a single character by default.

      so
      > opts, args = getopt.getopt(argv,"pdb:list:",["pfile=","lfile="])

      should be:
      > opts, args = getopt.getopt(argv,"p:l:",["pfile=","lfile="])

      And your assignment should be then become:

      > if opt in ("-p","--pfile"):
      and
      > if opt in ("-l","--lfile"):

      The drawback of using argparse is that this module is not standard in Python 2.6.
      Last edited by R.W.W.Brouwer; 05-01-2014, 11:49 PM. Reason: typos, typos and more typos

      Comment

      • bio_informatics
        Senior Member
        • Nov 2013
        • 182

        #4
        @R.W.W.Brouwer - Thank you for pointing out the issue. I didn't know about single character.

        @blancha - I didn't know for argparse. I will try that too.
        Bioinformaticscally calm

        Comment

        Latest Articles

        Collapse

        • SEQadmin2
          Nine Things a Sample Prep Scientist Thinks About Before Sequencing
          by SEQadmin2


          I’m not a sequencing expert. I’m a purification scientist who uses NGS to evaluate workflows my group develops. With this perspective, we think about the sample first and the NGS workflow second. The sequencer is an exceptionally honest reporter, but it can only report on what you give it, so whether you get clean, interpretable data from an NGS workflow is largely determined before you begin.


          Here are nine questions we think about, in roughly the order they matter, before...
          Yesterday, 07:11 AM
        • SEQadmin2
          From Collection to Sequencing: Why Sample Preparation and Preservation Define Sequencing Data
          by SEQadmin2


          Data variability is still an issue in sequencing technologies despite the advances in reproducibility and accuracy of these platforms. But the problem does not originate in the sequencing itself, but in the previous steps, before the sample reaches the sequencer.


          The first step is collection, followed by preservation and sample preparation for analysis. Most scientists overlook those steps, but not being careful might just be skewing the experiment’s results.
          ...
          06-02-2026, 10:05 AM
        • SEQadmin2
          Single-Cell Sequencing at an Inflection Point: Early Impacts of New Platforms and Emerging Trends
          by SEQadmin2


          With the launch of new single-cell sequencing platforms in 2026, the field stands at an exciting inflection point. This article surveys the most impactful advances in the field and discusses how they’re reshaping research in cancer, immunology, and beyond.


          Introduction

          Single-cell sequencing technologies have undergone remarkable advances over the past decade, transitioning from low-throughput experimental approaches to highly scalable platforms capable of...
          05-22-2026, 06:42 AM

        ad_right_rmr

        Collapse

        News

        Collapse

        Topics Statistics Last Post
        Started by SEQadmin2, 06-17-2026, 06:09 AM
        0 responses
        20 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 06-09-2026, 11:58 AM
        0 responses
        38 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 06-05-2026, 10:09 AM
        0 responses
        44 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 06-04-2026, 08:59 AM
        0 responses
        49 views
        0 reactions
        Last Post SEQadmin2  
        Working...