Build variable into python zip script
Question: Before I start, I am trying to create a python zip script which will take a
snapshot of the target_dir, zip it, save it in the temp folder and give it the
filename of "now" variable. This is the code I have:

    
    
    #!/usr/bin/env python
    
    import os
    import sys
    import datetime
    
    now = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M")
    target_dir = '/var/lib/data'
    temp_dir='/tmp'
    
    zip = zipfile.ZipFile('/tmp/example.zip', 'w', zipfile.ZIP_DEFLATED)
    rootlen = len(target_dir) + 1
    for base, dirs, files in os.walk(target_dir):
       for file in files:
          fn = os.path.join(base, file)
          zip.write(fn, fn[rootlen:])
    

I can't figure out how to get this line to instead use the variable "now" and
"temp_dir" instead of hardcoding the destination:

    
    
    zip = zipfile.ZipFile('/tmp/example.zip', 'w', zipfile.ZIP_DEFLATED)
    

I guess I want something like this (pseudo code):

    
    
    zip = zipfile.ZipFile('<temp_dir>/<now>.zip', 'w', zipfile.ZIP_DEFLATED)
    

Can anyone show me how this should be done?


Answer: In short, `os.path.join(temp_dir, now + '.zip')`, but I'm not sure if that's
really what you're asking. Because I'm not sure how you could have gotten as
far as you have and gotten stuck here.

