[Assorted-commits] SF.net SVN: assorted:[980] python-commons/trunk/src/commons/files.py
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-10-02 19:13:51
|
Revision: 980 http://assorted.svn.sourceforge.net/assorted/?rev=980&view=rev Author: yangzhang Date: 2008-10-02 19:13:44 +0000 (Thu, 02 Oct 2008) Log Message: ----------- added read_file, write_file, write_or_rm, is_nonempty_file from 6.00 code Modified Paths: -------------- python-commons/trunk/src/commons/files.py Modified: python-commons/trunk/src/commons/files.py =================================================================== --- python-commons/trunk/src/commons/files.py 2008-10-02 18:47:14 UTC (rev 979) +++ python-commons/trunk/src/commons/files.py 2008-10-02 19:13:44 UTC (rev 980) @@ -169,3 +169,22 @@ else: # cache up-to-date (should be available since dlcs-timestamp exists!) with file(cache_path) as f: return load(f) + +def read_file(path): + f = file(path) + try: return f.read() + finally: f.close() + +def write_file(path, contents): + f = file(path,'w') + try: f.write(contents) + finally: f.close() + +def write_or_rm(p, contents): + 'Write the file or remove it if contents is empty.' + p = path(p) + if contents.strip(): write_file(p, contents) + elif p.isfile(): p.remove() + +def is_nonempty_file(path): + return path.isfile() and read_file(path).strip() != '' This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |