# The sqlite3 lets you create and use # a database with just a file import sqlite3 # more detailed python doc sqlite3 import os # in this example we get the current working dir path # Choose the file to use for the # db and connect (create it) conn = sqlite3.connect(os.path.abspath('.') + "tempdb") # grab a cursor and we can create the db schema c = conn.cursor() # if you happen to run through this example a few times # you may notice that the data is persistant. For this example # we'll ensure that we're starting from ground zero # drop the database (if it exists) c.execute('drop table if exists users') # create a table c.execute('create table users (name text, age text, email text)') # insert data c.execute("""insert into users values ('steve', '30', 'blah@blah.com')""") c.execute("""insert into users values ('steve2', '32', 'blah@blah2.com')""") c.execute("""insert into users values ('steve3', '33', 'blah@blah3.com')""") #, # ('steve II', '20', 'blah2@blah.com'), # ('steve III', '10', 'blah3@blah.com')""") # now lets select our data c.execute('select * from users') # iterate through the results with for each for row in c: print row # output: # (u'steve', u'30', u'blah@blah.com') # (u'steve2', u'32', u'blah@blah2.com') # (u'steve3', u'33', u'blah@blah3.com')
A python example based blog that shows how to accomplish python goals and how to correct python errors.
Showing posts with label path. Show all posts
Showing posts with label path. Show all posts
Saturday, October 3, 2009
Python - using sqlite3 module for persistant data
Thursday, October 1, 2009
Python - using glob to get lists of files and directories
import os.path # glob is a simple and useful python module. # It uses simple regular expressions to match # directories and files for a given path. If # you've ever used the command line to 'ls' or # 'dir' the currently directory you may be aware # that the directory accepts * or ? or [] to # match patterns. glob is a python implementation # of this functionality. import glob import os # find all the .txt files in the current working directory print glob.glob('*.TXT') # output: # ['LICENSE.txt', 'NEWS.txt', 'README.txt'] # you can also specify a full path # Here I'm searching for dll files in python 2.6 print glob.glob('C:\Python26\DLLs\*.dll') # output: # ['C:\\Python26\\DLLs\\sqlite3.dll', # 'C:\\Python26\\DLLs\\tcl85.dll', # 'C:\\Python26\\DLLs\\tclpip85.dll', # 'C:\\Python26\\DLLs\\tk85.dll'] # If you are expecting a great deal of results # you should use the glob.iglob method that returns # matches as it goes and does not load everything # into memory first. # glob.iglob() example f = glob.iglob('C:\Python26\Lib\*') spitItOut = True while spitItOut: try: fileNameAndPath = f.next() # since glob gives you the full path you can # use the output with some of the os module's methods if os.path.isfile(fileNameAndPath): fileNameAndPath += " is a file." else: fileNameAndPath += " is not a file." print fileNameAndPath except StopIteration: spitItOut = False #output (snipped a bit...since there a lot): # C:\Python26\Lib\abc.py is a file. # ....[snip] # C:\Python26\Lib\compiler is not a file. # ...[another snip] # C:\Python26\Lib\getopt.py is a file. # C:\Python26\Lib\getopt.pyc is a file. # C:\Python26\Lib\getpass.py is a file. # C:\Python26\Lib\gettext.py is a file. # C:\Python26\Lib\glob.py is a file. # C:\Python26\Lib\glob.pyc is a file.
Tuesday, September 22, 2009
python - os module and working directory
import os
# curdir attribute lists the current directory
# which is always '.' .... which means...right here
print os.curdir
#output:
# .
# to see what the path to the curdir
print os.path.abspath(os.path.curdir)
#output
# C:\Documents and Settings\steve\My Documents\python
# and then to see what files are in the curdir
print os.listdir(os.curdir)
#or
print os.listdir(os.path.abspath(os.path.curdir))
# output for either listdir
#['colormaker.py', 'createThumbnail.py', 'strFunctions.py', 'pycolor.py']
Subscribe to:
Posts (Atom)