Sie sind auf Seite 1von 6

1. 2. 3. 4.

# a look at file handling in Python # tested with Python24 vegaseat 29sep2005

# set up a test string str1 = """There is no ham in hamburger. Neither apple nor pine are in pineapple. Boxing rings are square. Writers write, but fingers don't fing. If a vegetarian eats vegetables, what does a humanitarian eat?

5.
6. 7. 8. 9.

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.
18. """ 19.

str2 = """An alarm goes off by going on.

17. Fill in a form by filling it out.

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. 23. 24.


25.

fout = open("English101.txt", "w") fout.write(str1) fout.close()

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. 29. 30. 31. 32.


33.

fin = open("English101.txt", "r") str3 = fin.read() fin.close() print "Contents of file English101.txt:" print str3

34. # a similar read using try/except error handling 35. # tested this out by deliberately changing the filename

36. 37. 38. 39. 40. 41. 42.

filename = "English102.txt" try: fin = open(filename, "r") str3 = fin.read() fin.close() print "Contents of file %s:" % filename print str3

43. 44.
45.

except IOError: print "File %s does not exist!" % filename

46. print 47. 48. # append more text to an existing file with modifier "a"

49. 50. 51.


52.

fout = open("English101.txt", "a") fout.write(str2) fout.close()

53. # read the appended text file as a list of lines with readlines()

54. 55. 56. 57. 58. 59.


60.

fin = open("English101.txt", "r") lineList = fin.readlines() fin.close() print "Contents of appended file (first option):" for line in lineList: print line,

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. 67. 68.


69.

print "Contents of appended file (second option):" for line in open("English101.txt", "r"): print line,

70. print 71. 72. # similar to above, but creating a list with list comprehension

73. 74. 75.


76.

line_list = [line for line in open("English101.txt", "r")] print "A list of the text lines:" print line_list # test

77. print 78. 79. # read just one line of text at a time

80. 81. 82. 83. 84.

print "The first two lines:" fin = open("English101.txt", "r") print "Line 1 =", fin.readline(), print "Line 2 =", fin.readline() # etc. fin.close()

85. 86. # show just the last line of text

87. 88. 89. 90. 91.


92.

fin = open("English101.txt", "r") lineList = fin.readlines() fin.close() print "Last line =", lineList[-1], print "Total lines =", len(lineList)

93. # the whole thing can be simplified (more cryptic though)

94. 95.
96.

lastLine = file("English101.txt", "r").readlines()[-1] print "Last line =", lastLine

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: f = open(filename) 141. f.close() 142. return True 143. 144. except: return False 145.
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: print "%02X" % ord(c), 184. 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()

Das könnte Ihnen auch gefallen