Python How to's Flashcards
How to take input from user ?
We have to use input function.
variable = input(“Enter Value >”)
How to take arguments from terminal ?
- Import optparse module
- Create optparse object.
- Teach optparse object for arguments.
- Capture arguments in variable using optparse.
import optparse
parser = optparse.OptionParser()
parser.add_option(“-i”, “–interface”, dest = “interface”, help = “Network Interface “)
(interface, arguments) = parser.parse_args()
subprocess.call(“ifconfig”, interface.interface)
How to Execute Linux System Command ?
- import subprocess module
- execute system command with subrocess functions
import subprocess
subprocess.call(“ifconfig”,”eth0”)
How to handel regular expressions ?
- import re module
- execute re functions to search
import re
re.search(r’regular expression’,stringVariable,flag)
How many ways to present list with index value?
By Three Ways we can achieve this.
1. Using enumerate function 2. Using Zi[p Function 3. Usinge range function
For Code See Juypter Lab - Python Exercise for Practice
How to use subprocess Module.
import subprocess
subprocess.call(“ifconfig”,interface,”hw”,”ether”)
How to capture output of subprocess.
ifconfig_output = subprocess.check_output(“ifconfig”, interface,)
How many types to use subprocess module.
subprocess. call(“ifconfig eth0 down “,shell=True)
subprocess. call(“ifconfig “+ interface + “ down”,shell=True)
subprocess. call(“ifconfig”,”eth0”)
subprocess. call(“ifconfig”, interface)
Which Function is used with subprocess to execute system command.
subprocess.call()
Which Function is used with subprocess to read output of system command.
subprocess.check_output
Which class is used to create optparse object?
optparse.OptionParser()
Which function is used with optparse object to teach object ?
parser.add_options(“-i”, “–interface”, dest = “interface”,help=”Ethernet Interface”)
Which function is used with optparse object to process arguments ?
parser.parse_args()
interface,arguments)=parse.parse_args(
Which function is used with optparse object to handle error ?
parser.error()
Which function is used with optparse object to get arguments ?
parser.parse_args()