#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

    Formatting output of print statements for commands


    I am a Python beginner. My python script runs a lot of commands like zip, copy, svn, etc. What I need to do is to show the users the output on my Linux console in the following form as the python scripts continues to execute the commands:
    Code:
    Copying project directory...Done!
    Zipping the project directory...Done!
    Committing files to SVN...Done!
    etc.
    Is there a best practice in python to do this?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    wing-n-sling


    Code:
    import time
    import sys
    
    w=sys.stdout.write
    f=sys.stdout.flush
    
    for (command,message,) in ((1,'Copying project directory...'),
                               (2,'Zipping the project directory...'),
                               (3,'Committing files to SVN...')):
        w(message)
        f()
        time.sleep(command) # simulate the action
        w('Done!\n')
    
    #w('Copying project directory...')
    #w('Done!\n')
    #w('Zipping the project directory...')
    #w('Done!\n')
    #w('Committing files to SVN...')
    #w('Done!\n')
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo