Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16884 > unrolled thread
| Started by | Frank van den Boom <frank@am-knie.de> |
|---|---|
| First post | 2011-12-08 23:41 +0100 |
| Last post | 2011-12-09 10:47 +0100 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.python
subprocess.Popen under windows 7 Frank van den Boom <frank@am-knie.de> - 2011-12-08 23:41 +0100
Re: subprocess.Popen under windows 7 Lie Ryan <lie.1296@gmail.com> - 2011-12-09 13:08 +1100
Re: subprocess.Popen under windows 7 Frank van den Boom <frank@am-knie.de> - 2011-12-09 09:42 +0100
Re: subprocess.Popen under windows 7 Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-12-09 09:32 +0100
Re: subprocess.Popen under windows 7 Tim Golden <mail@timgolden.me.uk> - 2011-12-09 09:28 +0000
Re: subprocess.Popen under windows 7 Frank van den Boom <frank@am-knie.de> - 2011-12-09 10:47 +0100
| From | Frank van den Boom <frank@am-knie.de> |
|---|---|
| Date | 2011-12-08 23:41 +0100 |
| Subject | subprocess.Popen under windows 7 |
| Message-ID | <jbref7$roj$1@news.albasani.net> |
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.
[toc] | [next] | [standalone]
| From | Lie Ryan <lie.1296@gmail.com> |
|---|---|
| Date | 2011-12-09 13:08 +1100 |
| Message-ID | <mailman.3462.1323396520.27778.python-list@python.org> |
| In reply to | #16884 |
On 12/09/2011 09:41 AM, Frank van den Boom wrote: > 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.
[toc] | [prev] | [next] | [standalone]
| From | Frank van den Boom <frank@am-knie.de> |
|---|---|
| Date | 2011-12-09 09:42 +0100 |
| Message-ID | <jbshln$ebi$2@news.albasani.net> |
| In reply to | #16891 |
>
> 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()
[toc] | [prev] | [next] | [standalone]
| From | Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> |
|---|---|
| Date | 2011-12-09 09:32 +0100 |
| Message-ID | <1lt8r8-rel.ln1@satorlaser.homedns.org> |
| In reply to | #16884 |
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
[toc] | [prev] | [next] | [standalone]
| From | Tim Golden <mail@timgolden.me.uk> |
|---|---|
| Date | 2011-12-09 09:28 +0000 |
| Message-ID | <mailman.3465.1323422892.27778.python-list@python.org> |
| In reply to | #16900 |
On 09/12/2011 08:32, Ulrich Eckhardt wrote: > 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
[toc] | [prev] | [next] | [standalone]
| From | Frank van den Boom <frank@am-knie.de> |
|---|---|
| Date | 2011-12-09 10:47 +0100 |
| Message-ID | <jbslfd$sop$1@news.albasani.net> |
| In reply to | #16901 |
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);>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web