background preloader

Python script

Facebook Twitter

Python Files I/O. This chapter covers all the basic I/O functions available in Python.

Python Files I/O

For more functions, please refer to standard Python documentation. Printing to the Screen The simplest way to produce output is using the print statement where you can pass zero or more expressions separated by commas. This function converts the expressions you pass into a string and writes the result to standard output as follows − #! This produces the following result on your standard screen − Python is really a great language, isn't it? Reading Keyboard Input Python provides two built-in functions to read a line of text from standard input, which by default comes from the keyboard.

Raw_inputinput The raw_input Function The raw_input([prompt]) function reads one line from standard input and returns it as a string (removing the trailing newline). #! This prompts you to enter any string and it would display same string on the screen. Enter your input: Hello PythonReceived input is : Hello Python The input Function #! Syntax Example. Index. How to send email in Python via SMTPLIB. Sending Emails Via Gmail SMTP With Python. This is the third post in the article series “Playing With Python And Gmail”.

Sending Emails Via Gmail SMTP With Python

This will be a tutorial on how to send mails using Python smtplib through Gmail SMTP. The smtplib module defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon. [vinod@mercury ~]$ python Python 2.5.2 (r252:60911, Jan 24 2010, 14:53:14) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information.

>>> import smtplib The first step is to create a SMTP connection to the server. The smtplib.SMTP class encapsulates an SMTP connection. Remember Google’s SMTP server is ‘smtp.gmail.com’ and the port is 587. Next we will identify ourself to an ESMTP server using EHLO. Next we call SMTP.starttls function to put the SMTP connection in TLS (Transport Layer Security) mode. OK, now we are safe to login to the server using SMTP.login(user, password). We will cerate some SMTP headers before sending our mail. Sample code. How to Send Email with Python. Where I work, we run a number of login scripts written in Python.

How to Send Email with Python

When an error occurs in one of those scripts, we want to know. So we wrote a simple Python script to email the error to us. Since then, I’ve needed to figure out a way to send attachments with some of my more advanced scripts. If you’re a long-time reader of this blog, then you may remember wxPyMail, which was a simple wxPython program that could send email. In this article, you’ll discover how to send email with just Python’s standard library.

Sending Email with smtplib Sending email with smtplib is super simple. Sending Emails Via Gmail SMTP With Python. Kutuma's Ramblings. Python main() functions. All Things PythonicPython main() functionsby Guido van van RossumMay 15, 2003 Summary For Python programmers, I've got some suggestions on how to write a main() function that's easy to invoke in other contexts, e.g. from the interactive Python prompt when you feel like experimenting. I've written a few main() functions in my time. They usually have a structure roughly like this: """Module docstring. This serves as a long usage message. """ import sys import getopt def main(): # parse command line options try: opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) except getopt.error, msg: print msg print "for help use --help" sys.exit(2) # process options for o, a in opts: if o in ("-h", "--help"): print __doc__ sys.exit(0) # process arguments for arg in args: process(arg) # process() is defined elsewhere if __name__ == "__main__": main() I'm sure many people write similar main() functions.

Note that we fill in the default for argv dynamically. How to Send Email with Python.