mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-03-10 16:30:40 +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
|
import os.path
|
||||||
from iter import ifilter
|
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=''):
|
def mktemp(suffix=''):
|
||||||
"""Gives a decent random string, suitable for a filename."""
|
"""Gives a decent random string, suitable for a filename."""
|
||||||
r = random.Random()
|
r = random.Random()
|
||||||
@ -64,6 +76,13 @@ def nonEmptyLines(fd):
|
|||||||
def nonCommentNonEmptyLines(fd):
|
def nonCommentNonEmptyLines(fd):
|
||||||
return nonEmptyLines(nonCommentLines(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):
|
class AtomicFile(file):
|
||||||
"""Used for files that need to be atomically written -- i.e., if there's a
|
"""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'"""
|
failure, the original file remains, unmodified. mode must be 'w' or 'wb'"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user