Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #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


    • #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


      • #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

        • seqadmin
          Current Approaches to Protein Sequencing
          by seqadmin


          Proteins are often described as the workhorses of the cell, and identifying their sequences is key to understanding their role in biological processes and disease. Currently, the most common technique used to determine protein sequences is mass spectrometry. While still a valuable tool, mass spectrometry faces several limitations and requires a highly experienced scientist familiar with the equipment to operate it. Additionally, other proteomic methods, like affinity assays, are constrained...
          04-04-2024, 04:25 PM
        • seqadmin
          Strategies for Sequencing Challenging Samples
          by seqadmin


          Despite advancements in sequencing platforms and related sample preparation technologies, certain sample types continue to present significant challenges that can compromise sequencing results. Pedro Echave, Senior Manager of the Global Business Segment at Revvity, explained that the success of a sequencing experiment ultimately depends on the amount and integrity of the nucleic acid template (RNA or DNA) obtained from a sample. “The better the quality of the nucleic acid isolated...
          03-22-2024, 06:39 AM

        ad_right_rmr

        Collapse

        News

        Collapse

        Topics Statistics Last Post
        Started by seqadmin, 04-11-2024, 12:08 PM
        0 responses
        21 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 04-10-2024, 10:19 PM
        0 responses
        23 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 04-10-2024, 09:21 AM
        0 responses
        18 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 04-04-2024, 09:00 AM
        0 responses
        49 views
        0 likes
        Last Post seqadmin  
        Working...
        X