File Handling with Python

Please support our Python advertiser: Programming Forums
Sep 30th, 2005
Views: 32,816
Thread Rating: 2 votes, 3.5000 average.
AddThis Social Bookmark Button
This snippet takes a look at Python file handling. Different ways to write and read text files, zipped files and memory streams. Investigates how to access only part of a file. Also explores the "read" of a binary image file and performs a hex-dump of the data.
python Syntax
  1. # a look at file handling in Python
  2. # tested with Python24 vegaseat 29sep2005
  3.  
  4. # set up a test string
  5. str1 = """There is no ham in hamburger.
  6. Neither apple nor pine are in pineapple.
  7. Boxing rings are square.
  8. Writers write, but fingers don't fing.
  9. If a vegetarian eats vegetables, what does a humanitarian eat?
  10. Overlook and oversee are opposites.
  11. Slim chance and fat chance are the same.
  12. A house can burn up as it burns down.
  13. """
  14.  
  15. # more to test file appending
  16. str2 = """An alarm goes off by going on.
  17. Fill in a form by filling it out.
  18. """
  19.  
  20. # let's create our test file by writing the test string to the working folder/directory with write()
  21. # modifier "w" is for writing text, use "wb" for binary data like images
  22. fout = open("English101.txt", "w")
  23. fout.write(str1)
  24. fout.close()
  25.  
  26. # read back the entire test string as a string with read()
  27. # "r" is for reading text, use "rb" for binary data like images
  28. fin = open("English101.txt", "r")
  29. str3 = fin.read()
  30. fin.close()
  31. print "Contents of file English101.txt:"
  32. print str3
  33.  
  34. # a similar read using try/except error handling
  35. # tested this out by deliberately changing the filename
  36. filename = "English102.txt"
  37. try:
  38. fin = open(filename, "r")
  39. str3 = fin.read()
  40. fin.close()
  41. print "Contents of file %s:" % filename
  42. print str3
  43. except IOError:
  44. print "File %s does not exist!" % filename
  45.  
  46. print
  47.  
  48. # append more text to an existing file with modifier "a"
  49. fout = open("English101.txt", "a")
  50. fout.write(str2)
  51. fout.close()
  52.  
  53. # read the appended text file as a list of lines with readlines()
  54. fin = open("English101.txt", "r")
  55. lineList = fin.readlines()
  56. fin.close()
  57. print "Contents of appended file (first option):"
  58. for line in lineList:
  59. print line,
  60.  
  61. print
  62.  
  63. # a short-form to do this, uses readlines() internally
  64. # the comma at the end of print takes care of the extra newline character
  65. # note: Python does clean up and closes the file for you
  66. print "Contents of appended file (second option):"
  67. for line in open("English101.txt", "r"):
  68. print line,
  69.  
  70. print
  71.  
  72. # similar to above, but creating a list with list comprehension
  73. line_list = [line for line in open("English101.txt", "r")]
  74. print "A list of the text lines:"
  75. print line_list # test
  76.  
  77. print
  78.  
  79. # read just one line of text at a time
  80. print "The first two lines:"
  81. fin = open("English101.txt", "r")
  82. print "Line 1 =", fin.readline(),
  83. print "Line 2 =", fin.readline() # etc.
  84. fin.close()
  85.  
  86. # show just the last line of text
  87. fin = open("English101.txt", "r")
  88. lineList = fin.readlines()
  89. fin.close()
  90. print "Last line =", lineList[-1],
  91. print "Total lines =", len(lineList)
  92.  
  93. # the whole thing can be simplified (more cryptic though)
  94. lastLine = file("English101.txt", "r").readlines()[-1]
  95. print "Last line =", lastLine
  96.  
  97. # do some random access of the file
  98. fin = open("English101.txt", "r")
  99. # seek index is zero based, so the 10th character would be position 9
  100. fin.seek(9)
  101. print "From character 10 to end of line =", fin.readline()
  102. print "End if this line is at character =", fin.tell()
  103. print
  104. num = 16
  105. pos = 80
  106. print "Read %d characters starting at position %d:" % (num, pos)
  107. fin.seek(pos)
  108. print fin.read(num)
  109. fin.close()
  110.  
  111. print
  112.  
  113. # read a particular line, lineNumber is zero based
  114. import linecache
  115. lineNumber = 5
  116. partLine = linecache.getline("English101.txt", lineNumber)
  117. print "Line %d = %s" % (lineNumber, partLine)
  118.  
  119. print
  120.  
  121. # processing the lines as you read them in and forming a list
  122. # using list comprehension
  123. list2 = [line.replace(".", "!") for line in open("English101.txt", "r")]
  124.  
  125. # display the result
  126. print "Processing the lines as you read them in ..."
  127. print "Each period has been replaced with an exclamation mark:"
  128. for line in list2:
  129. print line,
  130.  
  131. print; print
  132.  
  133. # print to a file (a different option of write)
  134. fout = open( "test1.txt", "w" )
  135. print >>fout, "I love Monte Python!"
  136. fout.close()
  137.  
  138. # a file exists if you can open and close it
  139. def exists(filename):
  140. try:
  141. f = open(filename)
  142. f.close()
  143. return True
  144. except:
  145. return False
  146.  
  147. # what does file object fin look like?
  148. filename = 'test1.txt'
  149. if exists(filename):
  150. fin = open(filename)
  151. print "file object =", fin
  152. print "file content =", fin.read()
  153. fin.close()
  154. else:
  155. print "File %s does not exist!" % filename
  156.  
  157. print
  158.  
  159. # for large text files you can write and read a zipped file (PKZIP format)
  160. # notice that the syntax is mildly different from normal file read/write
  161. import zipfile
  162. zfilename = "English101.zip"
  163. zout = zipfile.ZipFile(zfilename, "w")
  164. zout.writestr(zfilename, str1 + str2)
  165. zout.close()
  166. # read the zipped file back in
  167. zin = zipfile.ZipFile(zfilename, "r")
  168. strz = zin.read(zfilename)
  169. zin.close()
  170. print "Testing the contents of %s:" % zfilename
  171. print strz
  172.  
  173. print
  174.  
  175. # read a binary image file, pick something you have ...
  176. # (also shows exception handling)
  177. filename = "Moo.jpg"
  178. try:
  179. fin = open(filename, "rb")
  180. data = fin.read()
  181. fin.close()
  182. print "This is a hex-dumb of %s:" % filename
  183. for c in data:
  184. print "%02X" % ord(c),
  185. print
  186. except IOError:
  187. print "Binary File %s not found" % filename
  188. #raise SystemExit # optional exit
  189.  
  190. print
  191.  
  192. # below is a typical Python dictionary object of roman numerals
  193. romanD1 = {'I':1,'II':2,'III':3,'IV':4,'V':5,'VI':6,'VII':7,'VIII':8,'IX':9,'X':10}
  194.  
  195. # to save a Python object like a dictionary to a file
  196. # and load it back intact you have to use the pickle module
  197. import pickle
  198. print "The original dictionary:"
  199. print romanD1
  200. file = open("roman1.dat", "w")
  201. pickle.dump(romanD1, file)
  202. file.close()
  203. # now load the dictionay object back from the file ...
  204. file = open("roman1.dat", "r")
  205. romanD2 = pickle.load(file)
  206. file.close()
  207. print "Dictionary after pickle.dump() and pickle.load():"
  208. print romanD2
  209.  
  210. print
  211.  
  212. # module StringIO allows you to treat a data stream like a file
  213. # if you do a lot of processing, memory streams are much faster then file streams
  214. # (StringIO.StringIO is a class that can be inherited in a class of your own)
  215. print "You can stream text/data to memory ..."
  216. import StringIO
  217. stream1 = StringIO.StringIO(str2) # use the string str2 here, or read one in from a file
  218. print str2
  219.  
  220. # show the memory where the object is located
  221. print stream1
  222.  
  223. print
  224.  
  225. print "... and use stream like a file:"
  226. print stream1.readline()
  227.  
  228. # reset the stream to zero (beginning) and read all lines
  229. stream1.seek(0)
  230. list1 = stream1.readlines()
  231. print "All lines from beginning:"
  232. for item in list1:
  233. print item,
  234. print
  235. # reset the stream to position 9 and read the next 20 characters
  236. stream1.seek(9)
  237. print "Read 20 characters starting at position 9:"
  238. print stream1.read(20)
  239.  
  240. # finally close the stream
  241. stream1.close()

Only community members can submit or comment on code snippets. You must register or log in to contribute.

Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:59 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC