Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
huhu
Feb 24, 2006
I've got a Python function that I've added throughout a script I want to run autonomously. Its purpose is to print various log information to a txt file. What would the bests way to log any errors that might occur to the same file?

code:
def print_log(text_to_log):
    """
    Print text to log.txt file and command line
    """
    print(text_to_log)
    f = open("log.txt", "a")
    f.write(text_to_log + "\n")
    f.close()

Adbot
ADBOT LOVES YOU

huhu
Feb 24, 2006

a witch posted:

Look into the logging module, it has a decent tutorial.

If you want to write messages to the console and a file at the same time, you'll want a logger with a FileHandler and a StreamHandler attached.

E: I'm on a phone so can't type code, here's the basic tutorial https://docs.python.org/3/howto/logging.html

Awesome thanks.

Another question - I have a script running that uploads files to dropbox. If the internet goes down mid upload, I get a MaxRetryError. Trying the following doesn't work though
code:
try:
    # upload file
    return True
except MaxRetryError:
    return False
what should I be putting instead?

huhu
Feb 24, 2006

Eela6 posted:

What do you want to happen?

I want the function to return false. However, with that code I'm getting "NameError: global name 'MaxRetryError' is not defined".

huhu
Feb 24, 2006

Zero Gravitas posted:

Anyone got any recommendations for a method of creating animations (avi/mpeg/mp4) from a bunch of .png files? I've inherited a collection of code that does this with cv2 (I think) but its awfully put together and throws lots of errors with the l/w/h parameters.

Virtualdub is pretty awesome. Just select fps, compression method, and any other settings and you get a nice video.

huhu
Feb 24, 2006
I have some Flask code:
code:
    {% for image in gallery %}
        <img src ="{{ url_for('static', filename = 'img/image') }}">
    {% endfor %}
gallery is a list containing a bunch of filenames (i.e. "a.jpg", "b.jpg") How do I pass image (i.e. "a.jpg") from the for loop into 'img/image' so that it appears as 'img/a.jpg' instead?

Edit - Is this the best way?
code:
    {% for image in gallery %}
        <img src ="{{ url_for('static', filename = 'img/')}}{{ image }}">
    {% endfor %}

huhu fucked around with this message at 03:14 on Mar 7, 2017

  • Locked thread