Skip to content

Instantly share code, notes, and snippets.

@jamtat
Created October 18, 2013 13:50
Show Gist options
  • Save jamtat/7041832 to your computer and use it in GitHub Desktop.
Save jamtat/7041832 to your computer and use it in GitHub Desktop.
A quick python script to append lines to the end of a file and create the file if it doesn't exist.
from os.path import abspath, exists
def appendToFile(filename, content):
f_path = abspath(filename)
mode = 'r+' if exists(f_path) else 'a+'
with open(f_path, mode) as f:
txt = f.read()
f.write(content+'\n')
# Example Usage
appendToFile(input('file: '), input('stuff to append: '))
# 20 minute coding challenges are fun!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment