mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-02-02 07:34:08 +01:00
Added a few useful file functions.
This commit is contained in:
parent
ca41962974
commit
c8d114b9bc
@ -36,6 +36,18 @@ import shutil
|
||||
import os.path
|
||||
from iter import ifilter
|
||||
|
||||
def writeLine(fd, line):
|
||||
fd.write(line)
|
||||
if not line.endswith('\n'):
|
||||
fd.write('\n')
|
||||
|
||||
def readLines(filename):
|
||||
fd = file(filename)
|
||||
try:
|
||||
return [line.rstrip('\r\n') for line in fd.readlines()]
|
||||
finally:
|
||||
fd.close()
|
||||
|
||||
def mktemp(suffix=''):
|
||||
"""Gives a decent random string, suitable for a filename."""
|
||||
r = random.Random()
|
||||
@ -64,6 +76,13 @@ def nonEmptyLines(fd):
|
||||
def nonCommentNonEmptyLines(fd):
|
||||
return nonEmptyLines(nonCommentLines(fd))
|
||||
|
||||
def chunks(fd, size):
|
||||
return iter(lambda : fd.read(size), '')
|
||||
## chunk = fd.read(size)
|
||||
## while chunk:
|
||||
## yield chunk
|
||||
## chunk = fd.read(size)
|
||||
|
||||
class AtomicFile(file):
|
||||
"""Used for files that need to be atomically written -- i.e., if there's a
|
||||
failure, the original file remains, unmodified. mode must be 'w' or 'wb'"""
|
||||
|
Loading…
Reference in New Issue
Block a user