subprocess.Popen under windows 7

Frank van den Boom frank at am-knie.de
Fri Dec 9 03:42:21 EST 2011


>
> I didn't have Windows 7 right now, but that shouldn't happen with the
> code you've given; when trimming code for posting, you should check that
> the trimmed code still have the exact same problem.
>

Here is the hole code:


#!/usr/bin/env python
# little script to backup recursive a folder with 7zip


SOURCE_DIR = "C:/Users/yoicks/Desktop/source"
DEST_DIR = "C:/Users/yoicks/Desktop/dest"
BACKUP_NAME_PREFIX = "BACKUP"
BACKUP_NAME_DELIMITER = "_"
METHOD = '7zip'
PATH_TO_7ZIP = "C:/Program Files/7-Zip/7z.exe"
PASSWORD = "1234"

import os, time, shutil, sys, tarfile, subprocess, traceback

try:
     # win32
     from msvcrt import getch
except ImportError:
     # unix
     def getch():
         import sys, tty, termios
         fd = sys.stdin.fileno()
         old = termios.tcgetattr(fd)
         try:
             tty.setraw(fd)
             return sys.stdin.read(1)
         finally:
             termios.tcsetattr(fd, termios.TCSADRAIN, old)

def press_any_key():
     print "Press any key to continue."
     getch()

def exit_with_string(exit_string):
     print exit_string
     press_any_key()
     sys.exit(exit_string)

def backup_directory_7zip(srcdir,archive_name):
     if os.path.exists(archive_name):
         exit_stop("backup path %s already exists!" % arcpath)
     try:
         # see 7zip help
         arglist = [PATH_TO_7ZIP,"a", "-sfx", archive_name, "*", "-r", 
"-p",PASSWORD]
         print ("try running cmd:\n %s\nin directory\n %s" % (' 
'.join(arglist),srcdir)) # join because i don't want [ ]
         sp = subprocess.Popen(args=arglist, stdout=subprocess.PIPE, 
stderr=subprocess.PIPE, cwd=srcdir)

         #output, error = subprocess.Popen(args=arglist, 
stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=srcdir).communicate()

     except:
         print "Error while running 7zip subprocess.\n"
         print "Traceback:\n%s"%traceback.format_exc()
         return False

     output, error = sp.communicate()
                                                             #something 
i tried output =  sp.stdout.read()
                                                             #somtehing 
i tried error = sp.stderr.read()
     if output:
         print output

     if error:
         print error

         return False
     return archive_name


# build backup name
print "start backup with python-script...\n"

timestr = time.strftime("%Y%m%d_%H%M%S",time.localtime())

if METHOD not in ["7zip"]:
     exit_stop("METHOD not '7zip'")

if not os.path.exists(SOURCE_DIR):
     exit_stop("SOURCE_DIR: %s doesn't exists" % 
os.path.abspath(SOURCE_DIR))

if not os.path.exists(DEST_DIR):
     exit_stop("DEST_DIR: %s doesn't exists" % os.path.abspath(DEST_DIR))

else:
     print("write backup from %s to %s \n using the %s method...\n" %
           (os.path.abspath(SOURCE_DIR), os.path.abspath(DEST_DIR), METHOD))
     if METHOD == "7zip":
         try:
             if not os.path.exists(PATH_TO_7ZIP):
                 exit_stop("Path to 7ZIP %s doesn't exist." % PATH_TO_7ZIP)
         except NameError:
             exit_stop("variable PATH_TO_7ZIP not defined")
         return_value = 
backup_directory_7zip(srcdir=os.path.abspath(SOURCE_DIR),
 
archive_name=os.path.abspath(os.path.join(
                                    DEST_DIR, BACKUP_NAME_PREFIX + 
BACKUP_NAME_DELIMITER + timestr + ".exe")))

if return_value:
     print("Backup successfully written.")
else:
     print("FAILURE during the backup")

press_any_key()


















More information about the Python-list mailing list