Python I/O

Reading line input from keyboard

>>> import sys
>>> a = sys.stdin.readline()
abcd
>>> print(a)
abcd

File I/O

>>> fh = open('now','r')	# Open a file for reading
>>> for line in fh:		# Print all lines
	print line,		# Note the comma
Now is the time
For all good men
To go to the aid
Of their country
>>> fh.seek(0)			# Position to BOF
>>> fh.readline()		# Print one line
'Now is the time\n'
>>> fh.readline()		# Print next line
'For all good men\n'
>>> fh.seek(0)			# Position to BOF
>>> print fh.readline()		# Another print method
Now is the time
>>> fh.seek(0)			# Position to BOF
fh.read(10)			# Print 10 characters
'Now is the'

>>> fh.closed			# Test for file closed
False
>>> fh.close			# Close the file
>>> fh.closed			# Test for file closed
True

Send mail to the Webmaster

logo This site best viewed with a browser
Warning: This is a Debian centric site and MAY contain peanuts.
Many thanks to Debra Lynn and Ian Murdock for making Debian possible
First created Aug 2, 2011 ~ Last revised August 07, 2011

Valid XHTML 1.0 Strict Valid CSS!