1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Python to zip and backup files but no files found

Discussão em 'Python' iniciado por Stack, Setembro 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am just beginning to learn Python and is following the example in the book "A Byte of Python". The example is to zip and backup files to a folder. The program did create a zip file but no file is stored inside. I tried running the same command within the Command Prompt and it is the same. Why is it so?

    Below is the code:

    import os
    import time


    source = ['C:\\Test', 'C:\\Code']

    target_dir = 'C:\\Backup'

    target = target_dir + os.sep + \
    time.strftime('%Y%m%d%H%M%S') + '.zip'

    if not os.path.exists(target_dir):
    os.mkdir(target_dir)


    zip_command = 'zip -r {0} {1}'.format(target,' '.join(source))

    print('Zip command is:' + '\n' + zip_command)
    print('Running:')
    if os.system(zip_command) == 0:
    print('Successful backup to', target)
    else:
    print('Backup FAILED!')


    Below is the output:

    Zip command is:
    zip -r C:\Backup\20250907205502.zip C:\Test C:\Code
    Running:
    adding: C:\Test/ (stored 0%)
    adding: C:\Test/New Microsoft Excel Worksheet.xlsx (deflated 15%)
    adding: C:\Test/New Microsoft Word Document.docx (stored 0%)
    adding: C:\Code/ (stored 0%)
    adding: C:\Code/New Text Document (2).txt (stored 0%)
    adding: C:\Code/New Text Document.txt (stored 0%)
    Successful backup to C:\Backup\20250907205502.zip


    Thank you.

    Continue reading...

Compartilhe esta Página