Hello ,

In this python script that do backup of folders , i just dont know why when i execute it printed :

Sauvegarde reussie vers E:\20130521
Echec de la sauvegarde

That means that this part :

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

# lancement de la sauvegarde

if os.system(zip_command) == 0:
    print 'Sauvegarde reussite vers', target

else:
    print 'Echec de la sauvegarde'

don't work on windows xp , how can i use zip command in python , this is all the script :

#!/usr/bin/python
#-*- coding:Utf-8 -*-

import os
import time
import sys


source = []

# 1. Les fichiers et repertoires a sauver sont indiques dans une liste.

source = ['C:\Documents and Settings\Administrateur.PC\Bureau\Python' ]

# Notez qu'il faut utiliser des double quotes a l'interieur la chaine pour les noms de fichiers avec des espaces.
 # 2. la sauvegarde est rangee dans un repertoire a part.

target_dir = 'E:'

today = target_dir + os.sep + time.strftime('%Y%m%d')
now = time.strftime('%H%M%S')

# Creer le sous-repertoire s'il n'existe pas

if not os.path.exists(today):
    os.mkdir(today) # cree le repertoire


 # 3. Les fichiers sont places dans une archive zip.
# 4. Le jour courant est le nom du sous-repertoire dans le repertoire principal

today = target_dir + os.sep + time.strftime('%Y%m%d')

# L'heure courante est le nom de l'archive zip

now = time.strftime('%H%M%S')

 # Creer le sous-repertoire s'il n'existe pas

if not os.path.exists(today):
    os.mkdir(today) # cree le repertoire

print 'Sauvegarde reussie vers', today

 # Le nom du fichier zip

target = today + os.sep + now + '.zip'

# 5. Nous utilisons la commande zip pour creer une archive

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

# lancement de la sauvegarde

if os.system(zip_command) == 0:
    print 'Sauvegarde reussite vers', target

else:
    print 'Echec de la sauvegarde'

Hello ,
Thank you pyTony , but i don't want to use that module zipfile , that was working on ubunto , but not in windows xp ..

With lines 13 and 18 like you have I doubt that it would work even in ubuntu, either use standard module like I suggested or install command line zip in Windows computer.

thank you for your answer , but i'm sure that this was working on ubuntu

i wanted to use the subprocess module also but i couldn't know how to use it , and got this Error :

WindowsError: [Error 2] Le fichier spécifié est introuvable

As PyTony points out, the code as given does not use the zipfile module, but rather calls on a separate program named 'zip' to perform the file operations. That is both unnecessary and non-portable. This is how it would work using the zipfile module:

import zipfile

zip = zipfile.ZipFile(target, 'w')

Now, to get the desired effect of zipping up a whole directory tree, you'd have to recurse through the whole directory tree and add each file in turn. However, if you have Python 2.7 or later, you can use the make_archive method of the shutil module instead:

import shutil

# where we are running in the root directory of the archive,
# and source is the archive's base directory 
make_archive(target, 'zip', '.', source)
shutil.make_archive

I guess

Oh, yes, if course, my apologies for that slip.

i wanted to use the subprocess module also but i couldn't know how to use it , and got this Error : WindowsError: [Error 2] Le fichier spécifié est introuvable

We don't know what the statment was that produced the error so can not offer any help on how to fix it. Please include the complete error message.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.