Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Python: How to Append to system path

import sys, os
sys.path.append('/game/perso/lib')
# Or
os.environ['TEST']="myprogram.exe"
# print custom variable
print print os.environ['TEST']
[/

Python: print a list of command-line arguments

import sys
print sys.argv

Python: Get current username

import os
print "Username: ",os.environ["USERNAME"]

Python: How to pass command-line arguments to a script

import sys
min=int(sys.argv[-2])
max=int(sys.argv[-1])
for x in range(min, max)
 print x, x*x

Python: Get current Working Directory

import os, sys
print 'my os.getcwd =>', os.getcwd( )            # show my cwd execution dir
print 'my sys.path  =>', sys.path[:10]            # sho

Python: Search files by extension

import fnmatch
import os
# find text files in current directory
for file in os.listdir("."):
    if fnmatch.fnmatch(file, "*.txt"):
        print f

Python: Getting the name of the default codec

import sys
# default codec used to encode and decode Unicode and string objects 
print sys.getdefaultencoding()

Python: Accessing Environment Variables

import os
print os.environ['PATH']
print os.environ['USERNAME'] #current session
print os.environ['Temp'] # Temporary directory
print os.environ['P

Python: platform-independent way to enter a password in a command-line program

import getpass
# get current username
usr = getpass.getuser()
pwd = getpass.getpass("enter password for user %s: " % usr)

Python: Getting Random Numbers

import random
for i in range(10):
    # random float: 0.0 <= number < 1.0
    print random.random(),
    # random float: 10 <= number < 50
    prin

Python: How to Format Dates and Times

import time
now = time.localtime(time.time())
print time.asctime(now)
print time.strftime("%y/%m/%d %H:%M", now)
print time.strftime("%c", now)
pri