Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8850967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:01:32+00:00 2026-06-14T13:01:32+00:00

Before I start, I am trying to create a python zip script which will

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:01:33+00:00Added an answer on June 14, 2026 at 1:01 pm

    Python string formating lets you put the content of variables into strings, that would be

    zip = zipfile.ZipFile('%s/%s.zip' % (temp_dir, now), 'w', zipfile.ZIP_DEFLATED)
    

    String formating replaces the occurences of “%s” with the strings in the tuple after the formating operator %.

    Alternatively (and cleaner), use the same os.path.join you use later in your code:

    zip = zipfile.ZipFile(os.path.join(temp_dir, now+".zip"), 'w', zipfile.ZIP_DEFLATED)
    

    os.path.join is a function that glues together elements of a file system path according to whatever logic your file system uses, so it would use \ instead of / in operating systems that use that character in paths. String concatenation is just +, so to glue the file ending .zip to another string now, now+".zip" just does the trick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to create a Python script that will autogenerate space-delimited arithmetic expressions
I am trying to figure out how to create a little Python script that
I am trying to create a class in python which reads the access key/secret
I'm trying to be a good developer and create some documentation before I start
I am trying to create a soundboard program which will play sounds using the
Before you start flaming, I'm going to tell you that I am trying to
I'm trying to run some unit-tests for NxBRE before I start referencing it's implementation
I'm trying to check to make sure an item is visible before I start
I am working on a Media up-loader which uploads images to Server. Before start
I am trying to create a pure-Python application bundle for a wxPython app. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.