Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Python passing filename through argument

Programming


Tags
argument, filename, python

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-10-2015   -   Original Discussion by yifangt
yifangt's Unix or Linux Image
yifangt yifangt is offline VIP Member  
UNIX.COM VIP Member
 
Join Date: Sep 2009
Last Activity: 7 December 2017, 5:47 PM EST
Location: Saskatchewan, Canada
Posts: 516
Thanks: 332
Thanked 8 Times in 8 Posts
Python passing filename through argument

Hello,
A python beginner question on passing filename thru argument. My code is:


Code:
#!/usr/bin/python

import sys, getopt
import os

def main(argv):
    try:
        opts, args = getopt.getopt(sys.argv[1:],"hi:o:ce", ["help", "inputfile", "outputdir", "containment", "error_tolerance"])
    except getopt.GetoptError:
        usage()
        print("Usage: %s -i inputfile -o outputdirectory -c containment -e error_tolerance" % sys.argv[0])
        sys.exit(2)

    for opt, arg in opts:
        if opt == '-h':
           print 'this_script.py -i <inputfile> -o <outputdir> -c <containment> -e <error_tolerance>'
           sys.exit()
        elif opt in ("-i", "--infile"):
           inputfile = arg    #Is this the right way to pass filename thru argument?
        elif opt in ("-o", "--outdir"):
           outputdir = arg
        elif opt == '-c':
           containment = arg
        elif opt == '-e':
           error_pct = arg

if __name__ == '__main__':
    main(sys.argv[1])


def get_overlap_data(m4_filename):
    containment_tolerance = containment
    permitted_error_pct = error_pct
    overlap_data = {}
    contained_reads = set()
    with open(m4_filename) as m4_f:
        for l in m4_f:
            # ......omitted
    return overlap_data, contained_reads

overlap_data, contained_reads = get_overlap_data(inputfile)   #This line always gave error
......

but I always got error as:

Code:
Traceback (most recent call last):
  File "learnpython01.py", line 123, in <module>
    overlap_data, contained_reads = get_overlap_data(inputfile)
NameError: name 'inputfile' is not defined

I was wondering what the right way it is to pass filename thru argument in python. Related lines are highlighted in red.
Thanks a lot!
Sponsored Links
    #2  
Old Unix and Linux 08-10-2015   -   Original Discussion by yifangt
sea's Unix or Linux Image
sea sea is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 14 October 2016, 2:49 PM EDT
Location: Swissh
Posts: 1,285
Thanks: 256
Thanked 226 Times in 209 Posts
With python, error debuging starts at the bottom - with the 'last' error found
Also, to parse options, you should loop through the arguments, and use a case statement to identify the current option name 'opt' (which was retrieved from arg[v]).

Have a look at: info getopt and continue reading (PGDN).
Also checkout man getopt, to get a possible location for code Examples.

Hope this helps
Sponsored Links
    #3  
Old Unix and Linux 08-10-2015   -   Original Discussion by yifangt
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 12 December 2017, 6:43 AM EST
Location: San Jose, CA, USA
Posts: 10,769
Thanks: 590
Thanked 3,769 Times in 3,217 Posts
Note also that inputfile is only defined if a -i inputfile option is present on the command line.

Maybe you want to define it with a default value in case that option is not present. Or, if there is no reasonable default, print an error message and exit if no -i option was found while parsing the options.
    #4  
Old Unix and Linux 08-10-2015   -   Original Discussion by yifangt
yifangt's Unix or Linux Image
yifangt yifangt is offline VIP Member  
UNIX.COM VIP Member
 
Join Date: Sep 2009
Last Activity: 7 December 2017, 5:47 PM EST
Location: Saskatchewan, Canada
Posts: 516
Thanks: 332
Thanked 8 Times in 8 Posts
What I expected to have the script working in following command line:

Code:
python learnpython01.py -i infile -o outdir -c 500 -e 2

Could not find examples on handling file I/O stream.
I have difficulty to understand string[file] I/O stream here. In the example code, the function prototype is
def get_overlap_data(m4_filename):
and, calling of the function is by
overlap_data, contained_reads = get_overlap_data(inputfile)
whereinputfile = arg from argument parsing.
I am not sure what I have missed among the connections, if any stream is involved.
Thanks any way!

Last edited by yifangt; 08-10-2015 at 06:17 PM..
Sponsored Links
    #5  
Old Unix and Linux 10-27-2015   -   Original Discussion by yifangt
goon12's Unix or Linux Image
goon12 goon12 is offline
Registered User
 
Join Date: Oct 2001
Last Activity: 19 March 2016, 12:02 PM EDT
Location: Worcester, MA
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
The inputfile name is defined inside the function main, so it is out of scope when you're calling
Code:
overlap_data, contained_reads = get_overlap_data(inputfile)

, so you could just call get_overlap_data from inside of main, after handling all the args.
Sponsored Links
    #6  
Old Unix and Linux 11-06-2015   -   Original Discussion by yifangt
yifangt's Unix or Linux Image
yifangt yifangt is offline VIP Member  
UNIX.COM VIP Member
 
Join Date: Sep 2009
Last Activity: 7 December 2017, 5:47 PM EST
Location: Saskatchewan, Canada
Posts: 516
Thanks: 332
Thanked 8 Times in 8 Posts
I regret posting this naive questions before I read the book(s). After some more reading and found out one solution:

Code:
overlap_data, contained_reads = get_overlap_data(sys.argv[1])

Need more about getopt() and argparse (as Aia pointed out below) in python.
Thanks to all who replied.

Last edited by yifangt; 12-22-2015 at 01:44 PM..
Sponsored Links
    #7  
Old Unix and Linux 11-07-2015   -   Original Discussion by yifangt
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 20 November 2017, 9:27 AM EST
Posts: 1,673
Thanks: 49
Thanked 641 Times in 601 Posts
Try to learn about argparse instead of getopt.
Some documentation why getopt or optparse might not be the right solution.
The Following User Says Thank You to Aia For This Useful Post:
yifangt (12-22-2015)
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Argument passing Roozo Shell Programming and Scripting 9 03-15-2013 07:19 AM
Help with passing argument dollypee Shell Programming and Scripting 7 11-22-2012 03:50 PM
passing Argument Shahul Shell Programming and Scripting 4 11-13-2008 08:50 AM
Problem with Argument Passing A_Rod Shell Programming and Scripting 4 09-13-2006 12:47 PM
pass argument to a filename jpprial UNIX for Dummies Questions & Answers 1 09-03-2001 04:12 PM



All times are GMT -4. The time now is 12:02 PM.