Tuesday, May 15, 2018

Python notes

A few interesting Python things that I always forget how to do. The problem is I don't write Python code on a daily basis, nor have I done so for many years like I did Perl, so I tend to forget things in between the times I do write it. I'll add more here as I come across them.

Converting int to a string inside a print statement, note the %(var)s part.

print("\nServices running on localhost:%(port)s\n" % {'port': port})

Catching CTRL-C:

def signal_handler(signal, frame):
        print('You pressed Ctrl+C!')
        print("Exiting...")
        sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

Full absolute path for a log file

logfile = join(dirname(normpath(abspath(__file__))), 'file.log')





No comments:

Post a Comment

Current Audible Reading List

Title You Never Forget Your First: A Biography of George Washington A Self-Made Man: The Politica...