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

Python: How write to a Memory File

import StringIO
file = StringIO.StringIO()
data="Welcom to home"
data1="
Python" * 1000
file.write(data)
file.write(data1)
print file.getvalue()
[/

Python: Get size in bytes of a file

import os
print os.path.getsize("FindFile.py")
print os.stat("FindFile.py")[6]
print os.stat("FindFile.py").st_size

Python: print the calendar for any given month

__author__="sako"
__date__ ="$6 oct. 2012 14:45:11$"
import calendar
def myCalendar():
    calendar.prmonth(2012, 10)
if __name__ == "__main__":

Python: Change the Working Directory

import os
cwd = os.getcwd()
print "1", cwd
os.chdir("samples")
print "2", os.getcwd()

Python: How to identify different image file formats

mport imghdr
file="photo.jpg"
result = imghdr.what(file)
if result:
    print "file format:", result
else:
    print "cannot identify file", file

Python: Hcheck if year is a leap year

import calendar
import time
def leapYear(year):
    calendar.isleap(year)
if __name__ == "__main__":
    print leapYear(2012)

Python: print current year calendar

import calendar
import time
def myCalendar():
    now = time.localtime(time.time())
    calendar.prcal(2012)
if __name__ == "__main__":
    myCalen

Python: How to read in-memory file object

import StringIO
data = "Java, Python, Perl How to program."
file = StringIO.StringIO(data)
print file.read()