Skip to content

Instantly share code, notes, and snippets.

@TheoChristiaanse
Last active May 18, 2018 18:35
Show Gist options
  • Save TheoChristiaanse/35ab9c6e19c2163c32513b433d7bee5f to your computer and use it in GitHub Desktop.
Save TheoChristiaanse/35ab9c6e19c2163c32513b433d7bee5f to your computer and use it in GitHub Desktop.
This code refreshes an output file. It will remove the file and you can resave the file later with new content. If it does not exist it will make the file to save into.
MIT License
Copyright (c) 2018 T.V. Christiaanse
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
import os
import numpy as np
# This file is make to take an existing data file and replace it with a new fresh file
# with new fresh data.
# Code is licensed under MIT license
# T. V. Christiaanse
# This saves a matrix of [nxm] content to a tab spaced out file
def FileSaveMatrix(filename, content):
with open(filename, "a") as f:
for line in content:
f.write(" ".join("{:9.6f}\t".format(x) for x in line))
f.write("\n")
# this saves one line of content to a file
def FileSave(filename, content):
with open(filename, "a") as myfile:
myfile.write(content)
# This refreshes the files content and header.
# if the file does not excist it will make a file for you.
def refreshFile(filename,content,header):
fN= filename
try:
os.remove(fN)
except IOError:
print("Making file")
open(fN, 'a').close()
FileSave(fN,header)
FileSaveMatrix(fN,content)
refreshFile(fN,content,header)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment