How to handle errors related to sys.argv[] in python ?

jax

I have write down a simple code to open a csv file that reads data frame. The code is able to print data frame. To make this code more interactive i have integrated a functionality so that it can raise an error if sys.argv[1] is not true. Unfortunately, i am not able to properly integrate the "raise exception part", after struggling too much I'm looking for a solution. The code is given bellow (Though, i have tried so many thing with the native code but here i am pasting only the last change that i have made to achieve my goal (i.e raise exception if sys.argv[1] is not True ): Thanks

class My_csv_class(object):

    def __init__(self):

        self.csv_path = sys.argv[1]

    def csv_open(self):

        try:
            self.r = pd.read_csv(self.csv_path)
        except IndexError:
            print "Cannot open the file: "
        else:
            return self.r 

    def print_r(self):

        self.r


if __name__=="__main__":

    a = My_csv_class()
    a.csv_open()
    a.print_r()
Martijn Pieters

The exception is raised in your __init__ method. You could put the try..except around the instance creation:

if __name__=="__main__":
    try:
        a = My_csv_class()
    except IndexError:
        print "You did not specify a file"
        sys.exit(1)
    a.csv_open()
    a.print_r()

However, it is more common to handle command line issues outside of such code. Keep your code re-usable without a command line and put all your 'script tasks' in the __main__ guarded code:

class My_csv_class(object):
    def __init__(self, filename):
        self.csv_path = filename

    def csv_open(self):
        self.r = pd.read_csv(self.csv_path)

    def print_r(self):
        self.r

if __name__=="__main__":
    try:
        filename = sys.argv[1]
    except IndexError:
        print "You did not specify a file"
        sys.exit(1)

    a = My_csv_class(filename)
    a.csv_open()
    a.print_r()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to access parameters of sys.argv Python?

From Dev

How to handle errors in python?

From Dev

How to set sys.argv in a boost::python plugin

From Dev

How to use python argparse with args other than sys.argv?

From Dev

How to use python argparse with args other than sys.argv?

From Dev

How to set sys.argv in a boost::python plugin

From Dev

Python sys.argv to preserve ' ' or ""

From Dev

Python sys.argv and argparse

From Dev

Python sys.argv to preserve ' ' or ""

From Dev

Python sys.argv scoping rules

From Dev

Access sys.argv as bytes in Python 3

From Dev

Python not printing all the sys.argv

From Dev

Python: skipping if the last sys.argv is empty

From Dev

Encoding sys.argv in python 2.7

From Dev

How can it be that len(sys.argv) <= 0?

From Dev

How to use sys.argv in python to check length of arguments so it can run as script?

From Dev

How can I get command line arguments(sys.argv) in python

From Dev

How to pass a numbers list to function using command line sys.argv Python3

From Dev

How to pass a numbers list to function using command line sys.argv Python3

From Dev

How to pass a numbers list to function using command line sys.argv Python3

From Dev

How can I get command line arguments(sys.argv) in python

From Dev

How to send args from one python file to other's sys.argv

From Dev

How to pass entire directory into python via command line and sys.argv

From Dev

handle errors in Python ArgumentParser

From Dev

handle errors in Python ArgumentParser

From Dev

python sys.argv[1] vs. sys.argv[1:]

From Dev

python sys.argv[1] vs. sys.argv[1:]

From Dev

How to handle 500 errors

From Dev

How to handle errors in GRequests?

Related Related

HotTag

Archive