def blankAFile(file_path):
'''
truncate a file to zero bytes, and preserve its original modification time
Adapted from 'Keeping Large intermediate files' (http://www.ruffus.org.uk/faq.html)
:param file: Input file path
:return: None
'''
if os.path.exists(file_path):
timeInfo = os.stat(file_path) # retrieve current time stamp of the file
try:
f = open(file_path,'w')
except IOError:
pass
else:
f.truncate(0)
f.close()
# change the time of the file back to what it was
os.utime(file_path,(timeInfo.st_atime, timeInfo.st_mtime))
print file_path + ' blanked to save disk-space.'
else:
print 'blankAFile: ' + file_path + ' not found.'
sys.exit(1)
评论列表
文章目录