subprocess.Popen under windows 7

  • Thread starter Frank van den Boom
  • Start date
F

Frank van den Boom

Hello,

i have something like this under windows 7:


print("try command...")

arglist = [PATH_TO_7ZIP,"a", "-sfx", archive_name, "*", "-r",
"-p",PASSWORD]

p = subprocess.Popen(args=arglist, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, cwd=srcdir)

output, error = p.communicate()

if output:
print output

print ("Eyerthing is good")

press_any_key_to_continue()



The script works, but there is a little problem.
When I double-click the python file, then the command line will open and
the script starts.
I can read "try command..." in the command line window under windows 7.
But then I have to enter the return key in order that the script will go on.
After I had entered the return key the script completed sucessfully and
I saw the output.

What can I do, to prevent pressing the return key?



Thanks.
 
L

Lie Ryan

What can I do, to prevent pressing the return key?

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.
 
U

Ulrich Eckhardt

Am 08.12.2011 23:41, schrieb Frank van den Boom:
arglist = [PATH_TO_7ZIP,"a", "-sfx", archive_name, "*", "-r",
"-p",PASSWORD]

The "*" is resolved by the shell, this is not a wildcard that gets
passed to the program. At least not normally, your case might be different.
if output:
print output

print ("Eyerthing is good")

Is that Python 2 or 3?

That said, if you reduced it to something that doesn't e.g. require 7zip
I'd happily run it on an XP system with Python 2.7 to tell you if it
works there or not. Doing so would also rule out any influence by 7zip,
just in case.

Uli
 
F

Frank van den Boom

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()
 
T

Tim Golden

Am 08.12.2011 23:41, schrieb Frank van den Boom:
arglist = [PATH_TO_7ZIP,"a", "-sfx", archive_name, "*", "-r",
"-p",PASSWORD]

The "*" is resolved by the shell, this is not a wildcard that gets
passed to the program. At least not normally, your case might be different.

"... not normally" == "... not on Unix". On Windows, the shell
doesn't do any wildcard expansion. The OP is asking about behaviour
on Windows 7.

TJG
 
F

Frank van den Boom

Thank you very much.

Now I have written a little c++ programm which produces some ouput.

And now it works fine.
There is something wrong with 7zip.exe and the arglist with *.
Tonight I will go on and hunt the error.


It should be Python 2.7


#!/usr/bin/env python

PATH_TO_EXE = "C:/Users/yoicks/Desktop/ausgabe.exe"

import os, shutil, sys, 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 start_exe (PATH_TO_EXE):
try:
arglist = [PATH_TO_EXE]
print ("try running cmd:\n %s\n" % (' '.join(arglist)))
sp = subprocess.Popen(args=arglist, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)

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

output, error = sp.communicate()

if output:
print output

if error:
print error
return False

return True

return_value = start_exe(PATH_TO_EXE)

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

press_any_key()


* Englisch - erkannt
* Englisch
* Deutsch

* Englisch
* Deutsch

<javascript:void(0);>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top