subprocess.Popen under windows 7

Discussion in 'Python' started by Frank van den Boom, Dec 8, 2011.

  1. 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.
    Frank van den Boom, Dec 8, 2011
    #1
    1. Advertising

  2. Frank van den Boom

    Lie Ryan Guest

    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.
    Lie Ryan, Dec 9, 2011
    #2
    1. Advertising

  3. 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
    Ulrich Eckhardt, Dec 9, 2011
    #3
  4. >
    > 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()
    Frank van den Boom, Dec 9, 2011
    #4
  5. Frank van den Boom

    Tim Golden Guest

    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
    Tim Golden, Dec 9, 2011
    #5
  6. 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);>
    Frank van den Boom, Dec 9, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. madpython

    subprocess.Popen on Windows

    madpython, Jun 23, 2006, in forum: Python
    Replies:
    3
    Views:
    8,208
    Uri Nix
    Jun 25, 2006
  2. Méta-MCI

    Windows, subprocess.Popen & encodage

    Méta-MCI, May 8, 2007, in forum: Python
    Replies:
    2
    Views:
    385
  3. Tim Golden
    Replies:
    0
    Views:
    449
    Tim Golden
    Oct 7, 2007
  4. Replies:
    8
    Views:
    731
  5. Werner F. Bruhin

    subprocess.Popen on Windows

    Werner F. Bruhin, Oct 20, 2008, in forum: Python
    Replies:
    1
    Views:
    304
    Eric Carlson
    Oct 20, 2008
Loading...

Share This Page