Python 3 I/O: Difference between revisions

From Wiki
Jump to navigation Jump to search
Line 60: Line 60:
y = 3.14
y = 3.14
z = "george"
z = "george"
print 'result = %d' % x                # prints: result = 42
print('result = %d' % x)                 # prints: result = 42
print 'answers are: %d %f' % (x,y)      # prints: answers are: 42 3.14
print('answers are: %d %f' % (x,y))      # prints: answers are: 42 3.14
print 'hello %s' % z                    # prints: hello george
print('hello %s' % z)                   # prints: hello george
</source>
</source>



Revision as of 17:46, 14 January 2014

Command-line arguments

Command line arguments are placed in the list sys.argv.

import sys

command = sys.argv[0]
firstArg = sys.argv[1]
secondArg = sys.argv[2]

There is also a getopt module.

More here: http://www.faqs.org/docs/diveintopython/kgp_commandline.html

Reading and writing files

output = open('/tmp/spam', 'w')    # Create output file ('w' means write).
input = open('data', 'r')          # Create input file ('r' means read).
S = input.read( )                  # Read entire file into a single string.
S = input.read(N)                  # Read N bytes (1 or more).
S = input.readline( )              # Read next line (through end-line marker).
L = input.readlines( )             # Read entire file into list of line strings.
output.write(S)                    # Write string S into file.
output.writelines(L)               # Write all line strings in list L into file.
output.close( )                    # Manual close (done for you when file collected).

Manipulating files

See this page for more: http://docs.python.org/lib/os-file-dir.html

import os
os.access(fileName, os.F_OK)       # returns True if file exists
os.access(fileName, os.R_OK)       # returns True if file is readable
os.access(fileName, os.W_OK)       # returns True if file is writeable
os.access(fileName, os.X_OK)       # returns True if file is executable
os.getcwd()                        # return a path of current working directory
os.mkdir(newName)                  # create a directory
os.listdir(dirName)                # directory listing
os.rename(oldName, newName)        # move/rename a file
os.remove(fileName)                # unlink a file

Parsing a text file line-by-line

for line in file('/tmp/test/txt', 'r'):
    do something with line, like
    args = line.strip().split()

print statement

print(spam, ham)               # Print objects to sys.stdout; add a space between.
print(spam, ham, end="")       # Same, but don't add newline at end of text.
print(spam, ham, file=myfile)  # Send text to myfile.write, not to sys.stdout.write.

Formatted printing

x = 42
y = 3.14
z = "george"
print('result = %d' % x)                 # prints: result = 42
print('answers are: %d %f' % (x,y))      # prints: answers are: 42 3.14
print('hello %s' % z)                    # prints: hello george

urllib module for web access

import urllib

# open a url for reading
page = urlopen('http://www.python.org')
page.readline(  )                       # '<HTML>\012'
# download a page directly
urlretrieve('http://www.python.org/', 'wwwpython.html')

HTML parsing example

# extracts urls from a web page
import HTMLParser, urllib, urlparse

class LinksParser(HTMLParser.HTMLParser):
    def __init__(self):
        HTMLParser.HTMLParser.__init__(self)
        self.seen = {}
    def handle_starttag(self, tag, attributes):
        if tag != 'a': return
        for name, value in attributes:
            if name == 'href' and value not in self.seen:
                self.seen[value] = True
                pieces = urlparse.urlparse(value)
                if pieces[0] != 'http': return
                print urlparse.urlunparse(pieces)
                return

p = LinksParser( )
f = urllib.urlopen('http://www.python.org/index.html')
BUFSIZE = 8192
while True:
    data = f.read(BUFSIZE)
    if not data: break
    p.feed(data)

p.close( )

XML parsing example

<catalog>
  <book isbn="1-56592-724-9">
    <title>The Cathedral &amp; the Bazaar</title>
    <author>Eric S. Raymond</author>
  </book>
  <book isbn="1-56592-051-1">
    <title>Making TeX Work</title>
    <author>Norman Walsh</author>
  </book>
  <!-- imagine more entries here... -->
</catalog>
import xml.sax, xml.sax.handler

class BookHandler(xml.sax.handler.ContentHandler):
  def __init__(self):
    self.inTitle = 0                                # handle XML parser events
    self.mapping = {}                               # a state machine model

  def startElement(self, name, attributes):
    if name == "book":                              # on start book tag
      self.buffer = ""                              # save ISBN for dict key
      self.isbn = attributes["isbn"]
    elif name == "title":                           # on start title tag
      self.inTitle = 1                              # save title text to follow

  def characters(self, data):
    if self.inTitle:                                # on text within tag
      self.buffer += data                           # save text if in title

  def endElement(self, name):
    if name == "title":
      self.inTitle = 0                              # on end title tag
      self.mapping[self.isbn] = self.buffer         # store title text in dict

parser = xml.sax.make_parser( )
handler = BookHandler( )
parser.setContentHandler(handler)
parser.parse('books.xml')
print handler.mapping

MySQL access

http://www.kitebird.com/articles/pydbapi.html

import MySQLdb

conn = MySQLdb.connect(host = "localhost", user = "barney", passwd = "xxxxxx", db = "bts")
cursor = conn.cursor()
cursor.execute("select the_date, close from stocks where issue = 'a'")
for row in cursor.fetchall():
  print row
cursor.close()
conn.close()

If using INNODB tables, you need to throw in a commit statement for the connection before closing it:

conn = MySQLdb.connect(host = "localhost", user = "barney", passwd = "xxxxxx", db = "schedule")
cursor = conn.cursor()
cursor.execute("insert into users (name, pass) values ('Herman', '2909e93f30ac902891093')")
cursor.close()
conn.commit()   <== don't forget this!
conn.close()

Windows Tip

When starting a command-line python script by clicking on it in Winodws, use the raw_input() function at the end to keep the window open. Hitting return will close it.

Send E-mail

import smtplib
import os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders

def sendMail(send_from, send_to, subject, text, files=[], server="localhost"):
  assert type(send_to)==list
  assert type(files)==list

  msg = MIMEMultipart()
  msg['From'] = send_from
  msg['To'] = COMMASPACE.join(send_to)
  msg['Date'] = formatdate(localtime=True)
  msg['Subject'] = subject

  msg.attach( MIMEText(text) )

  for f in files:
    part = MIMEBase('application', "octet-stream")
    part.set_payload( open(file,"rb").read() )
    Encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
    msg.attach(part)

  smtp = smtplib.SMTP(server)
  smtp.sendmail(send_from, send_to, msg.as_string())
  smtp.close()