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

    Join Date
    Jul 2003
    Posts
    29
    Rep Power
    0

    passing file to program


    Hi folks,

    how are well all today... its bloody freezing here at the moment in Sydney

    if i have a file on the HD and want to pass it to a python script i use the sys.argv[x] which is fairly straight forward, however im trying to pass a file directly to my script after its been fetched via a pop email retrieve utility.

    i figured that sys.argv[x] still would of done that regardless if the file was on the HD or being piped from another program.

    im lost now so some help would be much appreciated.

    many thanks...
  2. #2
  3. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    12
    Again, I'd suggest you post your script. What are having problems with, exactly ? Is it with Python file I/O ?
    Time is the greatest of teachers ; sadly, it kills all of its students.
    - Hector Berlioz
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    29
    Rep Power
    0
    My apoligies solarbear...

    Code:
    #!/usr/bin/env python
    
    import email, os, sys, smtplib, string
    
    if len(sys.argv) == 1:
        print "Usage: %s filename" % os.path.basename(sys.argv[0])
        sys.exit(1)
    
    file = open(sys.argv[1], 'r')
    msg = email.message_from_file(file)
    file.close()
    
    fromaddr = 'MAILER-DAEMON'
    toaddrs  = msg['from']
    subject = 'ERROR'
    
    msgg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (fromaddr, toaddrs, subject))
    
    msgg = msgg + 'testing for now'
    
    server = smtplib.SMTP('localhost')
    #server.set_debuglevel(1)
    server.sendmail(fromaddr, toaddrs, msgg)
    server.quit()
    As it standards the code works fine if the email is allready on the HD but like i said if the email is piped directly to the script it fails.

    I have tried many senarios with no success

    thanks
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69

    pipeing stdin


    Hey sissy,

    When working with pipes you can read them in though stdin (sys.stdin). Take a look at this article for more info.

    http://www.linuxjournal.com/article.php?sid=3946

    Did you get my reply to your email?

    Have fun,
    Mark.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    206
    Rep Power
    12
    It has nothing to do with your question, but for how long do you live in Australia? Stupid question I know ... but a friend is in Sydney at moment ... perhaps you no him (improbably I know...)
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    29
    Rep Power
    0
    thanks Mark, i got ya email and have replied twice doesnt seem like your getting them?

    Wizard, i have lived in Sydney for all my life 32years in fact
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Hi sissy, i got one of your replies yesterday, have just replied. You mentioned error trapping, this is really easy with python. if you put out code in one or more try-except blocks you can provide alternatives actions if spacific errors occure.

    Mark.

IMN logo majestic logo threadwatch logo seochat tools logo