Recording the history of Nellore

Write a program to find the random numbers from 1 to 100 and call the function for 500 times and print how many times each number has been repeated

import random

def myrandom():

return random.randint(1,100)

#2. Call the function 500 times and save the results into a list.

r=[]

for x in range(500):

r.append(myrandom())

#3. Print how many times each number has repeated

for x in set(r):

print( x,r.count(x))

Write a program to print the size of the file and the time file was created

import os,time

size=os.stat(“C:/Users/exercise.py”).st_size

ctime=os.stat(“C:/Users/exercise.py”).st_ctime

print (“Size of the file :”,size)

print (“Created :”,ctime)

print (“Current time: “,time.time())

print (“Current time – Created time :”,time.time() – ctime)

Example for datetime module

import datetime

d1 = datetime.date(2008, 3, 12)

print(‘d1:’, d1)

d2 = d1.replace(year=2015,day=4,month=10)

d3=d1.replace(day=10)

d4=d1.replace(month=10)

print(‘d2:’, d2)

print(‘d3:’,d3)

print(‘d4:’,d4)

Example of datetime module

import datetime

today = datetime.date.today()

print (‘Today :’, today)

one_day = datetime.timedelta(weeks=2)

yesterday = today – one_day

print( yesterday)

print (‘Yesterday:’, yesterday)

tomorrow = today + one_day

print( ‘Tomorrow :’, tomorrow)

print( ‘tomorrow – yesterday:’, tomorrow – yesterday)

print( ‘yesterday – tomorrow:’, yesterday – tomorrow)

Example of os module functions

import os

print(os.getcwd())                   #get working dir

os.chdir(“c:\python27”)       #change dir

print(os.getcwd())                   #get working dir

os.system(“notepad.exe”)    #run a os command from python

Write a program to compress all py files from a folder and write the files in to zip file

import os

import zipfile

import glob

myhome=”c:\users\pythonfiles”

myzip=”c:\users\pythonfiles.zip”

#go to the home directory

os.chdir(myhome)

#open an empty zip file

z=zipfile.ZipFile(myzip,’w’)

#select all the *.py files

pyfiles=glob.glob(“*.py”)

#compress and write the files into the zip file

for file in pyfiles:

print(“Zipping file {}”.format(file))

z.write(file)

#close zip file

z.close()

print(“Zipping Complete”)

## Discover more from Nellorean

Subscribe now to keep reading and get access to the full archive.

Continue reading