Lab 2.
1 – Exercises for Python Language Development
Introduction
This set of exercises will allow the students to develop scripting skills in Python.
Software/Equipment needed
Python 3.6 or higher
Python IDLE on Windows or CLI, Ubuntu and Gedit
Windows or Linux OS
Tasks to complete activity
Lab Exercise 2.1
1.Create a python script to determine whether or not the shared file from the remote PC exists on
the network.
import os
path="//192.168.56.1//Users"
if os.path.exists(path):
print("yes")
else:
print("no")
2. Create a python script to verify the status of services on Linux and Windows computers.
import wmi
conn=wmi.WMI()
for service in conn.Win32_Service(StartMode="Auto", State="Running"):
print(s.State,s.StartMode,s.Name,s.DisplayName)
3. Write a python script to get the system information such as system name, login name, processor
details and operating system running on your computer.
import platform
print(platform.system())#os on computer
print(platform.processor())#processor
print(platform.node())#system name
import getpass
print(getpass.getuser())#login name
4. Write a python script a platform independent script to clear terminal.
import platform
import os
if platform.system()=="Windows":
os.system("cls") #for cmd
else:
os.system("clear") #for linux
#check the os and then use the right commands to clear terminal
5. Write a python script to find the files which are older than 5 days.
import os
import sys
import datetime
days=5
path=input("Enter your path:")
if not os.path.exists(path):
print("provide valid path")
sys.exit(1)
if os.path.isfile(path):
print("provide directory path")
sys.exit(2)
today=datetime.datetime.now()
for i in os.listdir(path):
flist=os.path.join(path,i)
if os.path.isfile(flist):
cdat=datetime.datetime.fromtimestamp(os.path.getctime(flist))
diff=(today-cdat).days
if diff>days:
print(flist,diff)
6. Write a python script to transfer a file from local pc to remote pc.
?
7. Write a python script to copy the content of a source file into a destination file.
sfile=input("source filepath:")
dfile=input("destination filepath:")
sfo= open(sfile,'r')
cont = sfo.read()
dfo=open(dfile,'w')
dfo.write(cont)
dfo.close()
8. Write a python script to read a path and check if given path is a file or dictionary.
import os
path=input("write path")
if os.path.isdir(path):
print("dictionary")
else:
print("file")
9. Write a python script to read and write to text files.
Sfile = input(“source filepath: ”)
Dfile = input("destination filepath: “)
Sfo = open(sfile, ‘r’)
Cont = sfo.read()
Sfo.close()
Dfo = open(dfile,’w’)
Dfo.write(cont)
10. Write a python script to find the size of the specific file.
Import os
Print(os.stat(“hi.txt”).st_size,”bytes”)