Skip to content

Commit

Permalink
Added readfilebytes and writefilebytes
Browse files Browse the repository at this point in the history
These methods are like readfile and writefile, but don't treat the
output/input as unicode respectively.
  • Loading branch information
fmoo committed Jul 4, 2015
1 parent 71819a9 commit 9896be9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -4,6 +4,7 @@ CHANGES
master
------
* PATENTS: Updated to version 2 (http://fb.me/patents2)
* fileutils: added `readfilebytes`, `writefilebytes` for raw binary bulk IO
* SelectTask: play more nicely with frameworks that steal sys.stdout

0.7.2
Expand Down
18 changes: 13 additions & 5 deletions sparts/fileutils.py
Expand Up @@ -17,15 +17,23 @@


def readfile(path):
"""Return the contents of the file at `path`"""
"""Return the contents of the file at `path`, as a string"""
return readfilebytes(path).decode('UTF-8')

def readfilebytes(path):
"""Return the contents of the file at `path`, as bytes"""
with open(path, mode='rb') as f:
return f.read().decode('UTF-8')
return f.read()

def writefile(path, contents):
"""Write `contents` to the file at `path`"""
logger.debug('writefile("%s", ...)', path)
"""Write string `contents` to the file at `path`"""
writefilebytes(path, contents.encode('UTF-8'))

def writefilebytes(path, contents):
"""Write bytes `contents` to the file at `path`"""
logger.debug('writefilebytes("%s", ...)', path)
with open(path, mode='wb') as f:
return f.write(contents.encode('UTF-8'))
return f.write(contents)

def makedirs(path, *args, **kwargs):
"""Create necessary directory heirarchy to `path` if it doesn't exist"""
Expand Down

0 comments on commit 9896be9

Please sign in to comment.