写日记用的 python 脚本

#!/usr/bin/python
import toot.utils
from datetime import datetime
import sys , os 
date_now = datetime.now().strftime('%Y-%m-%d %H:%M:%S\n')
a = toot.utils.editor_input('nano',
                            date_now
                            )
a = a.strip("\n ") # strip leading and ending empty lines AND spaces

if a == date_now.strip("\n "):  
    print("Abort input.")
else:
    file = os.getenv('HOME') +'/diary.txt'

    with open(file,'r') as f:
        old_content = f.read()


    with open(file, 'w') as f:
        f.write(a+'\n\n'+old_content)

用了 toottoot.utils

新了解的是 strip 方法可以接受多个 characters。

(method) def strip( self: Self@str, chars: str | None = None, / ) –> str Return a copy of the string with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead.

大概是,把要剥掉的 character 都写在一个字符串里。删除开头结尾的空格和空行就是 .strip("\n ")

日记不是指这里写的日记