The aim of it is to be the quickest way to add to existing notes via SSHh.

Is this code in Python style? Am I missing any methods that would make the code simpler or legible?

#! /usr/bin/python3

#Maintain: [email protected]
#Purpose: A simple way to add entries to existing files
#Usage: Write the name of the file you intend to add to.
#Future: A list of editable files when the program opens

from sys import argv
import datetime
import os,errno
import os.path
import contextlib

script, typed = argv
filename = typed+".txt"
savelocation = os.path.join('/home/user/',filename)
print("Aditor: a digital typewriter, Type: d when done")

date = str(datetime.datetime.now().date())
with open("/tmp/tempfile.txt","a+") as target:
        while True:
                if entry in ['done','d']: 

target = open("/tmp/tempfile.txt","r")
f = open(filename, "a")

print("Entry saved.")

with contextlib.suppress(FileNotFoundError):
  • 1
    \$\begingroup\$ Why do you need to write the contents to a temporary file in the first place, instead of opening the output file with "a" directly? \$\endgroup\$ Mar 25, 2017 at 22:20
  • \$\begingroup\$ that's a good point, my thinking was, if my connection drops before I am done, it won't affect my the original notes file, but now that you brought it up, it might not make a difference if I write directly to the note. \$\endgroup\$
    – OrigamiEye
    Mar 25, 2017 at 22:23
  • \$\begingroup\$ That's a fair point, but tangentially, if you're worried about your ssh connection dropping, you should use tmux or screen. \$\endgroup\$ Mar 25, 2017 at 22:38

1 Answer 1


If you don't want to write to the file directly and want to "apply" the changes at once, instead of writing to a temp file, you can keep the changes in memory - in a list of strings:

from sys import argv
from datetime import datetime
import os

if __name__ == '__main__':
    _, typed = argv
    filename = typed + ".txt"
    save_location = os.path.join('/home/user/', filename)

    print("Aditor: a digital typewriter, Type: d when done")

    date = datetime.now().strftime("%Y/%m/%d")

    cache = ["\n", date]
    while True:
        entry = input()

        if entry == 'done' or entry == 'd':


    with open(save_location, "a") as f:

Note that I've removed everything related to handling the temp file.

Also there were some other changes applied in the code above:

  • using 4 spaces for indentation
  • used _ for a "throwaway" variable (the script name is not used)
  • fixed the variable naming - words need to be separated with an underscore - save_location instead of savelocation (PEP8 reference)
  • checking for "done" using simple comparisons with done and d
  • using strftime() to dump a datetime into string
  • putting the code to under if __name__ == '__main__': to avoid the code to be executed if imported
  • organized imports per PEP8 (reference)
  • 1
    \$\begingroup\$ Thanks, I don't know when I would of otherwise started looking at cache and the other techniques you used. \$\endgroup\$
    – OrigamiEye
    Mar 26, 2017 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.