@@ -154,9 +154,8 @@ def dump_png(infile, pnglen, maxlen, pngid):
154
154
if nice_open_file (outname ):
155
155
buf = infile .read (pnglen )
156
156
print ("Writing PNG file" , outname )
157
- outfile = open (outname , "wb" )
158
- print (buf , end = ' ' , file = outfile )
159
- outfile .close ()
157
+ with open (outname , "wb" ) as outfile :
158
+ print (buf , end = ' ' , file = outfile )
160
159
else :
161
160
print ("PNG image %s too large (%i instead of maximal %i bytes), "
162
161
"file not written." % (pngid , pnglen , maxlen ))
@@ -350,9 +349,8 @@ def fill_directory(infile, txtfile, contents, firstclust, makedir, start,
350
349
startclust += 1
351
350
adstart += 0x1000
352
351
filelen -= 0x1000
353
- outfile = open (outname , "wb" )
354
- print (buf , end = ' ' , file = outfile )
355
- outfile .close ()
352
+ with open (outname , "wb" ) as outfile :
353
+ print (buf , end = ' ' , file = outfile )
356
354
357
355
do_utime (outname , dati2 , dati1 )
358
356
@@ -526,9 +524,9 @@ def getFileOrURL(filename, url):
526
524
# If so, return its contents. If not, download it, save it, and return its
527
525
# contents.
528
526
try :
529
- f = open (filename )
530
- print ("Found" , filename , "cached on disk, using local copy" )
531
- retval = f .read ()
527
+ with open (filename ) as f :
528
+ print ("Found" , filename , "cached on disk, using local copy" )
529
+ retval = f .read ()
532
530
return retval
533
531
except IOError :
534
532
pass
@@ -545,19 +543,18 @@ def getFileOrURL(filename, url):
545
543
print ("Reading response..." )
546
544
retval = response .read ()
547
545
# Save downloaded file to disk
548
- f = open (filename , "wb" )
549
- f .write (retval )
550
- f .close ()
546
+ with open (filename , "wb" ) as f :
547
+ f .write (retval )
551
548
print ("done, saved to" , filename )
552
549
return retval
553
550
554
551
def extractPirsFromZip (systemupdate ):
555
552
print ("Extracting $SystemUpdate/FFFE07DF00000001 from system update "
556
553
"file..." )
557
554
updatefile = StringIO .StringIO (systemupdate )
558
- z = zipfile .ZipFile (updatefile )
559
- # print(z.namelist())
560
- pirs = z .open ("$SystemUpdate/FFFE07DF00000001" ).read ()
555
+ with zipfile .ZipFile (updatefile ) as z :
556
+ # print(z.namelist())
557
+ pirs = z .open ("$SystemUpdate/FFFE07DF00000001" ).read ()
561
558
print ("done." )
562
559
return pirs
563
560
0 commit comments