More Basics
eharetea

More Python basics.

Some python basics.

Date Created:Friday December 29th, 2006 03:41 AM
Date Modified:Wednesday July 30th, 2008 11:47 PM

# this takes two lists and creates a dictionary

num = range(-4,9)

mun = range(-3,8)
mun.append(9)
mun.append(10)

for (x,y) in zip(mun,num):
     print 'content[',x,']=',y
     content[x] = y




# when importing, this helps you find out what a module contains
# info on all modules: http://docs.python.org/modindex.html
import os
dir(os)





# this prints the time

>>> import time
>>> print time.ctime()
Tue Aug  8 00:02:39 2006
>>> print time.time()
1155020791.73
>>> print time.strftime("%y/%m/%d %H:%M:%S", time.localtime())
06/08/08 00:04:49




# makes a list of current directory

import dircache
dircache.listdir('.')




# does a file exist?
import os
os.path.exists('./SRC')

# other os.path features: http://docs.python.org/lib/module-os.path.html

>>> os.path.getmtime('./SRC')
1154930867
>>> os.path.getctime('./SRC')
1154930867
>>> os.path.getsize('./SRC')
512L
>>> os.path.isfile('./SRC')
False
>>> os.path.isdir('./SRC')
True




# get system info
import sys
sys.version




# get length of a list
dan
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> len(dan)
19




#Step through a Dictionary's keys

name = {}
name['first_name'] = 'Daniel'
name['middle_name'] = 'Patrick'
name['last_name'] = 'Lynch'
for x in dan: print name[x]

#Step Through tuples in a list.

>>> listone = (1,2)
>>> listtwo = (3,4)
>>> listthree = (5,6)
>>> listall = [listone,listtwo,listthree]
>>> listall
[(1, 2), (3, 4), (5, 6)]
>>> for x,y in listall: print x,y
...
1 2
3 4
5 6
>>>    




# replace

import os
thepath = os.path.abspath('.')
npath = thepath.replace('/',' ')
npath
' usr home dlynch'
   
# find 

thepath.find('use')
-1
thepath.find('usr')
1
thepath.find('home')
5
    

  



# use the delete statement to delete 

del dan[1]
del dan['name']




# equivelent to ls -la 

import dircache
list = dircache.listdir('.')
for x in list:
     x