Created
October 18, 2013 13:50
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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