Python class command line

Call Python class methods from the command line

You’d create an instance of the class, then call the method:

test_instance = test(filepath) test_instance.method() 

Note that in Python you don’t have to create classes just to run code. You could just use a simple function here:

import sys import csv def read_csv(filepath): list = [] with open(self.filepath, "r") as table: reader = csv.reader(table, delimiter="\t") for line in reader: list.append[line] if __name__ == '__main__': read_csv(sys.argv[1]) 

where I moved the function call to a __main__ guard so that you can also use the script as a module and import the read_csv() function for use elsewhere.

Open Python interpreter from the command line.

Import your python code module, make a class instance and call the method.

>>> import test >>> instance = test(test_file) >>> instance.method() 
  • How to call class methods from different instantiations in parallel in Python 2.7?
  • Python (2.7) and reading Unicode argvs from the Windows command line
  • passing clipboard contents to python script run from the command line
  • Python Error: Able to run the program from command line but not from IDE
  • Python code that doesn’t work from the command line
  • Creating a Python function that opens a textfile, reads it, tokenizes it, and finally runs from the command line or as a module
  • Why does the super() call work in a Python class that derives from an ABC?
  • I am trying to print the last line of every file in a directory using shell command from python script
  • how to call a python function and give its parameters from the command line?
  • Sed working from the command line but not within a Python script
  • To call a function from a class, does Python iterate through every function the class has until it finds the function to call?
  • Python best way to setup global logger and set the logging level from command line
  • python call method from outside the class
  • how to call a the class from a method within the class in python to redefine a variable?
  • Unable to call functions from a class in python. Learning python the hardway ex49
  • How to get the count of command line parameters in python regardless of python keyword?
  • How to execute a class method from another class with the method passed a parameter in Python
  • Why does this python script only fail when run from the command line?
  • How to control odoo 10 from command line while its running in the web
  • How do I run my pycharm file in the command line using arguments from the user?
  • How can I get the variable that a python file with a path prints when I call that python file from a shell script?
  • How to call a method from base class a few levels up in the inheritance chain?
  • Object match error when running a Python script from command line Maya
  • python: how to run a program with a command line call (that takes a user’s keystroke as input) from within another program?
  • execute makefile from the command line not from sh.exe
  • Passing command line arguments from powershell script to a python script
  • Searching a text file for a line of text matching the text inputted from a user Python
  • I am trying to call a python script recommend.py from my PHP file. Is it the correct way ? No results are obtained
  • want to delete line number which is not process from file in python and append unprocess data at the end of file
  • Python — Extract string from a text file until the first 2 new line space
  • Passing the members/variables from python script to a shell script or a command
  • Trying to call a Python 3 script from a batch file when Python 2 is the default
  • How would I call a function within a class from the same class in python?
  • Run command line program from within Python
  • I am trying to import a set of lists in python and call a random item from one of the lists
  • MySQLdb module works when running Python script from the command line, but not as a cron job
  • Is it possible to run java command line app from python in AWS EC2?
  • How to call a linux command in Python without waiting for the results (Linux)
  • How to replace command line argument of filename with python string of the equivalent file content
  • Call a python function from java, while the python script was already running
  • Getting (in Python) the text output of executing a CMD command from Python
  • Importing methods from Python class into another class instance
  • How to open cygwin from python script, execute commands and take data from command line in file
  • Compile Cython extensions from the command line with gcc (mingw32) on windows
  • Hide os.system command line call in python script
  • Command to re-run script from within the script in python
  • what is the different about subprocess.call in python between direct command line input?
  • How to pass variables between methods in the same class in Python
  • How to start python unit tests from command line with Sikuli 1.0.1
  • python — calling a staticmethod from another class, having only the name of the other class given
Читайте также:  Options general php page

More Query from same tag

  • How to use property of object. Python!
  • kivy image collider size way too big
  • Move files to newly created folders more efficiently
  • Extract all characters between _ and .csv
  • How to install Numpy & pip3 for python3.x when they were installed in python2.7? Using Conda?
  • How to create new sheets inside a CSV file
  • (Python) Pygame: render text with special characters ( × ÷ π etc)
  • Extract certain portion of multiline string using Python re
  • Pytest raises exception but assert doesn’t catch it, although they are the same type and same value
  • Programmatically Merge PNG image into a PDF at an X/Y coordinate
  • Cannot start new python thread
  • How to include VCS information in setuptools packages
  • Error with Cassandra’s create_keyspace_simple
  • I am trying to find the nth binary palindrome
  • Webscrape e-commerce
  • Cache Proxy Server Returning 404 with www.google.com
  • Why does my python’s socket.shutdown work on Windows but not Ubuntu?
  • Creating a list of lists Python
  • csv to netCDF produces .nc files 4X larger than the original .csv
  • python nested list comprehension in nltk
  • How to convert HH:MM:SS time to decimal in Python
  • «are you sure» exit prompt in python tkinter GUI
  • Typehinting an unpack assignment
  • Bar plot showing same year and rating repeatedly
  • Comparing 2 lists and printing the differences
  • How to find keys related to maximum value in Java
  • os function to both join directories and create them?
  • How to restrict user input to a range of integers — python 3.6
  • Get average by word in column
  • How to fix dictionary value redefinition not changing the value of another list?
  • Save a list of tuple to csv file in python
  • Reading from a text file into a dictionary with variable columns
  • What does module linking in python mean
  • Customized style support in PySide, how to cast QStyleOption to it’s subclass?
  • Loop up and down to print «-» and «*»
Читайте также:  Javascript iframe onload this

Источник

Оцените статью