Python Inputs and Outputs
Getting user inputs is an essential part of a program. In python we have several ways to get inputs like direct keyboard , arguments, environment variables etc. In this document I'll explain about keyboard inputs and arguments.
We can get a keyboard input using input() function.
#!/usr/bin/env python x = str(input("enter a string : ")) y = int(input("enter a value : ")) print ("you entered %s for x and %d as y " % (x , y))
[email protected]:~$ python3 new.py enter a string : hello enter a value : 99 you entered hello for x and 99 as y
[email protected]:~$ python3 new.py enter a string : 5 enter a value : hello Traceback (most recent call last): File "new.py", line 3, in y = int(input("enter a value : ")) ValueError: invalid literal for int() with base 10: 'hello'
Do you have ever used tools like SQLMap , nmap, dirb etc. Then you give arguments to those programs while starting them.
nmap -sv 192.168.56.101
Here we used console arguments. both -sv and the IP address are arguments to the nmap program. We can get same input using a input function. But if you get inputs with arguments you don't want to get inputs after starting program.
So how we can use these arguments in our programs? Let's see.
#!/usr/bin/env python from sys import argv x,y,z = argv print ('first argument is %s' %(x)) print ('second argument is %s' %(y)) print ('third argument is %s' %(z))
[email protected]:~/programming$ python3 tmp1.py Thilan Danushka first argument is tmp1.py second argument is Thilan third argument is Danushka
You can see , it's working g fine on both python versions. There are some points to note. First you can see we use a special command "import". Those are related with modules (or libraries) . from sys import argv We say import argv function from the module sys. We will talk more about this in a later tutorial. Yes when we calling the program we supplied some arguments. python tmp1.py Thilan Danushka Python think all things after the word 'python' are arguments so it thinks all these three strings ('tmp1.py' , 'Thilan' , 'Danushka' ) are arguments. :-). All of our argument string will save in a array called argv. So in our program we extract those from argv. Next there is a command. x,y,z = argv Hear we get arguments from argv and assign them to variables called x,y and z. So variable x will contain the string 'tmp1.py' , Also 'Thilan' is in y and variable z contains the string 'Danushka' . Next we can print them or do what ever we want with those arguments. You can't assume user always input what you expect. He/she may enter a string while you say to enter an integer. So if you try to convert a string to integer it will throw an error and program will give unexpected results. Hear something to clear up. I said if program try to convert a string to integer it'll lead to an error. Then what happen when we use int() function? think about following example. x = '2' y = 'dummy_string' Hear both of variable x and y are strings. But we can covert x into a integer using int(x). What happen if we try to convert y into a integer?
>>> x='2' >>> int(x) 2 >>> y='dummy_text' >>> int(y) Traceback (most recent call last): File "", line 1, in ValueError: invalid literal for int() with base 10: 'dummy_text'
To avoid this we want to use expectations and error handling. I'll explain this in another tutorial. Thanks for reading.
Hi, I'm Thilan from Srilanka. An undergraduate Engineering student of University of Ruhuna. I love to explorer things about CS, Hacking, Reverse engineering etc.