0% found this document useful (0 votes)
9 views3 pages

Assignment No 1

The document contains four programming assignments involving Python and shell scripting. The first assignment captures an image from a webcam, converts it to grayscale, and logs the timestamp and file size in a CSV file. The other assignments involve calculating average student grades from a CSV file, backing up a directory while logging disk usage, and compressing a directory while listing its contents.

Uploaded by

sumedhnaiduakula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Assignment No 1

The document contains four programming assignments involving Python and shell scripting. The first assignment captures an image from a webcam, converts it to grayscale, and logs the timestamp and file size in a CSV file. The other assignments involve calculating average student grades from a CSV file, backing up a directory while logging disk usage, and compressing a directory while listing its contents.

Uploaded by

sumedhnaiduakula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment No 6

1. Write a Python script that captures an image from a


webcam, converts it to grayscale, and saves it. Use
pandas to log the timestamp of when the image was
taken and the file size of the saved image in a CSV file.
Ans. import cv2
import pandas as pd
import os
from datetime import datetime
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cap.release()
if ret:
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
filename = 'captured_image.jpg'
cv2.imwrite(filename, gray_frame)
file_size = os.path.getsize(filename)
log_data = {'Timestamp': [datetime.now()], 'File Size (bytes)':
[file_size]}
df = pd.DataFrame(log_data)
df.to_csv('image_log.csv', mode='a', index=False,
header=False)
print(f"Image saved as {filename} with size {file_size}
bytes.")
else:
print("Failed to capture image.")
2. Write a script that reads a CSV file containing student grades,
calculates the average grade, and writes the results to a new
CSV file.
Ans. import pandas as pd
df = pd.read_csv('student_grades.csv')
df['Average Grade'] = df.mean(axis=1)
df.to_csv('average_grades.csv', index=False)
print("Average grades calculated and saved.")

3. Write a shell script that backs up a directory and logs the disk
usage before and after the backup.
Ans. src_dir="/path/to/source"
dest_dir="/path/to/destination"
log_file="backup_log.txt"
echo "Backup started at $(date)" >> $log_file
echo "Disk usage before backup:" >> $log_file
df -h >> $log_file
cp -r $src_dir $dest_dir
echo "Backup completed at $(date)" >> $log_file
echo "Disk usage after backup:" >> $log_file
df -h >> $log_file
echo "Backup and logging completed."
4. Write a Python script that calls a shell script to compress a
directory and then lists the contents of the compressed file.s
Ans. import subprocess
import os
subprocess.call(['./compress_dir.sh'])
compressed_file = 'compressed_dir.tar.gz'
subprocess.call(['tar', '-tzf', compressed_file])

You might also like