Shebang, Input and ArgParse in Python (Python Part 11)

This class shows you how to make your scripts more interactive for users with the input() function, argparse module and shebangs.

  • 00:00 Intro
  • 04:39 Shebang
  • 11:29 Input() Function
  • 19:28 Argparse Module for Arguments
  • 25:42 Network Monitor Demo with Shebang and Argparse
  • 34:34 Final Thoughts

shebang-hello.py

#!/usr/bin/env python3

print('Hello there fellow Geeks!')

input.py

message = input('What is your message: ')

print(f'you said "{message}"')

input-add.py

num1 = input('num1: ')
num2 = input('num2: ')

answer1 = num1 + num2

answer2 = int(num1) + int(num2)

print(answer1)

print(answer2)

arg-input.py

import argparse

parser = argparse.ArgumentParser(description="Add 2 Numbers")
parser.add_argument('first', help='first number', type=str)
parser.add_argument('second', help='second number', type=str)
args = parser.parse_args()

print(args)

answer1 = args.first + args.second
print(answer1)

answer2 = int(args.first) + int(args.second)
print(answer2)

arg-ping.py

#!/usr/bin/env python3

import argparse
import os
from time import sleep

parser = argparse.ArgumentParser(description="Ping a Host")
parser.add_argument('host', help='Enter an IP or Host Name', type=str)
args = parser.parse_args()

while True:
  try:
    command = f'ping -c 1 {args.host}'
    response = os.popen(command)
    response = response.read()
  except:
    response = 'FAIL'

  os.system('clear')
  print(command)
  print(response)
  sleep(1)

Be the first to comment

Leave a Reply