Files I/O

Python 3 – Files I/O



Introduction :


This chapter covers all the basic I/O functions available in Python 3. For more functions, please refer to the standard Python documentation.


Printing to the Screen


The simplest way to produce output is using the print statement where you can pass zero or more expressions separated by commas. This function converts the expressions you pass into a string and writes the result to standard output as follows-


#!/usr/bin/python3
print ("Python is really a great language,", "isn't it?")

This produces the following result on your standard screen-

Python is really a great language, isn't it?


Reading Keyboard Input


Python 2 has two built-in functions to read data from standard input, which by default comes from the keyboard. These functions are input() and raw_input()

In Python 3, raw_input() function is deprecated. Moreover, input() functions read data from keyboard as string, irrespective of whether it is enclosed with quotes ('' or "" ) or not.


The input Function


The input([prompt]) function is equivalent to raw_input, except that it assumes that the input is a valid Python expression and returns the evaluated result to you.

Invoke interactive mode :

>>> x=input("something:")
something:10

>>> x
'10'

>>> x=input("something:")
something:'10'                #entered data treated as string with or without ''

>>> x
"'10'"