mirror of
https://github.com/python/cpython.git
synced 2025-07-15 23:35:23 +00:00
Added readfile() and readopenfile() functions.
This commit is contained in:
parent
261cbb2165
commit
17d82ce78a
2 changed files with 46 additions and 4 deletions
25
Lib/util.py
25
Lib/util.py
|
@ -1,9 +1,30 @@
|
|||
# Module 'util' -- some useful functions that dont fit elsewhere
|
||||
# Module 'util' -- some useful functions that don't fit elsewhere
|
||||
|
||||
# Remove an item from a list at most once
|
||||
|
||||
# Remove an item from a list.
|
||||
# No complaints if it isn't in the list at all.
|
||||
# If it occurs more than once, remove the first occurrence.
|
||||
#
|
||||
def remove(item, list):
|
||||
for i in range(len(list)):
|
||||
if list[i] = item:
|
||||
del list[i]
|
||||
break
|
||||
|
||||
|
||||
# Return a string containing a file's contents.
|
||||
#
|
||||
def readfile(fn):
|
||||
return readopenfile(open(fn, 'r'))
|
||||
|
||||
|
||||
# Read an open file until EOF.
|
||||
#
|
||||
def readopenfile(fp):
|
||||
BUFSIZE = 512*8
|
||||
data = ''
|
||||
while 1:
|
||||
buf = fp.read(BUFSIZE)
|
||||
if not buf: break
|
||||
data = data + buf
|
||||
return data
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue