This is my first ever Python program and I wanted to get everyone's opinions on it. The goal of the program is to separate an email address into its username and domain name.
import sys
import getopt
argv = sys.argv[1:]
# -------Error Handling-Start----------
try:
opts, args = getopt.getopt(argv, "l:s:h", ["list ", "single ", "help "])
except getopt.GetoptError as err:
print(f"{err}\n""\n"
"Usage: <Options> <Input> \n"
"-l, --list Specify a list of emails to be sliced \n"
"-s, --single Specify a single email to be sliced \n"
"-h, --help Show options")
opts = []
# -------Error Handling-End------------
for opt, file_arg in opts:
if opt in ['-h', '--help']:
sys.exit("Usage: <Options> <Input> \n"
"-l, --list Specify a list of emails to be sliced \n"
"-s, --single Specify a single email to be sliced \n"
"-h, --help Show options")
# If option -h is present, display MAN page
for opt, email_arg in opts:
if opt in ['-s', '--single']:
email = email_arg
username = email[:email.index('@')]
domain = email[email.index('@') + 1:]
print(f"Your username: {username}")
print(f"Your domain name: {domain}\n")
# If option -s is present, split input email into username and domain then print the output
for opt, file_arg in opts:
if opt in ['-l', '--list']:
file = file_arg
email_file = open(file, "r")
for string in email_file:
username = string[:string.index('@')]
domain = string[string.index('@') + 1:]
print(f"Your username: {username}")
print(f"Your domain name: {domain}")
# If option -l is present read file specified then loop through each line while splitting each into username and domain
if len(sys.argv) == 1:
sys.exit("Usage: <Options> <Input> \n"
"-l, --list Specify a list of emails to be sliced \n"
"-s, --single Specify a single email to be sliced \n"
"-h, --help Show options")
# If only one argument is supplied, print MAN page.