Mad readfile() read the file in one fell swoop.

This commit is contained in:
Guido van Rossum 1990-10-24 16:40:15 +00:00
parent 276123d1dc
commit f49ef1cad0
1 changed files with 11 additions and 1 deletions

View File

@ -41,7 +41,17 @@ def getstatusoutput(cmd):
# Return a string containing a file's contents. # Return a string containing a file's contents.
# #
def readfile(fn): def readfile(fn):
return open(fn, 'r').read(posix.stat(fn)[stat.ST_SIZE]) st = posix.stat(fn)
size = st[stat.ST_SIZE]
if not size: return ''
try:
fp = open(fn, 'r')
except:
raise posix.error, 'readfile(' + fn + '): open failed'
try:
return fp.read(size)
except:
raise posix.error, 'readfile(' + fn + '): read failed'
# Make command argument from directory and pathname (prefix space, add quotes). # Make command argument from directory and pathname (prefix space, add quotes).