Saturday, May 22, 2021

Writing to a file, and file creation/making a copy of a file

Script

#!/usr/local/bin/python3.5

import os
import time
from datetime import datetime

print("Current directory")
print("-----------------")
os.chdir('/Python-scripts/Memorable-Tests')
print(os.getcwd())

print("Creating a new file by name Old-Trafford-1990-K25.txt...")
with open('Old-Trafford-1990-K25.txt', 'w') as oldtrafford:
  pass

headingley = open('/Python-scripts/Memorable-Tests/Headingley-2002-K11.txt', 'w')
print()
print("Filename: ", headingley.name)
print("Mode of opening the file: ", headingley.mode)
print()
headingley.write('Rahul Dravid\'s most accomplished innings in England, a gritty 148 under severe seaming and swing conditions. \n')
headingley.write('Rahul Dravid took any number of body blows but was unflinching, handling the England bowlers with aplomb. \n')
headingley.write('He negotiated the seam and swing from Flintoff, Hoggard, Caddick and Tudor the entire first day of the Test. \n\n')
headingley.close()

print("----------------------------------------------------------------------------------")

print("Copy the file Headingley-2002-K11.txt to a new file...")
print("------------------------------------------------------")
with open('Headingley-2002-K11.txt', 'r') as headingley:
  with open('Dravid-master-class-148.txt', 'w') as dravid_master_class:
  for line_by_line in headingley:
  dravid_master_class.write(line_by_line)

print("----------------------------------------------------------------------------------")

print("Make a copy of a picture file")
print("-----------------------------")
os.chdir('/Python-scripts')
with open('KiranDS-picture.key', 'rb') as source_file:
  with open('KiranDS-picture-Backup.key', 'wb') as target_file:
  for every_bit in source_file:
  target_file.write(every_bit)

print("----------------------------------------------------------------------------------")

print("Make a copy of a picture file using chunks of data")
print("--------------------------------------------------")
with open('KiranDS-picture.key', 'rb') as source_file:
  with open('KiranDS-picture-Second-Backup.key', 'wb') as target_file:
  chunk_size = 2048
source_file_chunk = source_file.read(chunk_size)
  while len(source_file_chunk) > 0:
  target_file.write(source_file_chunk)
  source_file_chunk = source_file.read(chunk_size)


Before running the script

ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts > ls -altr KiranDS*
-rw-r--r--. 1 ec2-user ec2-user 1171927 Feb 28 21:52 KiranDS-picture.key
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts > cd Memorable-Tests/
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests > ls -altr Headingley*.*
ls: cannot access Headingley*.*: No such file or directory
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests > ls -altr Dravid*.*
ls: cannot access Dravid*.*: No such file or directory
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests > ls -altr Old-Trafford*.*
ls: cannot access Old-Trafford*.*: No such file or directory
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >


Execution

Current directory
-----------------
/Python-scripts/Memorable-Tests
Creating a new file by name Old-Trafford-1990-K25.txt...

Filename:  /Python-scripts/Memorable-Tests/Headingley-2002-K11.txt
Mode of opening the file:  w

----------------------------------------------------------------------------------
Copy the file Headingley-2002-K11.txt to a new file...
------------------------------------------------------
----------------------------------------------------------------------------------
Make a copy of a picture file
-----------------------------
----------------------------------------------------------------------------------
Make a copy of a picture file using chunks of data
--------------------------------------------------

After running the script

ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts > ls -altr KiranDS*.*
-rw-r--r--. 1 ec2-user ec2-user 1171927 Feb 28 21:52 KiranDS-picture.key
-rw-r--r--. 1 root     root     1171927 Feb 28 22:48 KiranDS-picture-Backup.key
-rw-r--r--. 1 root     root     1171927 Feb 28 22:48 KiranDS-picture-Second-Backup.key
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts > cd Memorable-Tests/
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests > ls -altr Headingley*.*
-rw-r--r--. 1 root root 328 Feb 28 22:48 Headingley-2002-K11.txt
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests > cat Headingley-2002-K11.txt
Rahul Dravid's most accomplished innings in England, a gritty 148 under severe seaming and swing conditions.
Rahul Dravid took any number of body blows but was unflinching, handling the England bowlers with aplomb.
He negotiated the seam and swing from Flintoff, Hoggard, Caddick and Tudor the entire first day of the Test.

ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests > ls -altr Dravid*.*
-rw-r--r--. 1 root root 328 Feb 28 22:48 Dravid-master-class-148.txt
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests > cat Dravid-master-class-148.txt
Rahul Dravid's most accomplished innings in England, a gritty 148 under severe seaming and swing conditions.
Rahul Dravid took any number of body blows but was unflinching, handling the England bowlers with aplomb.
He negotiated the seam and swing from Flintoff, Hoggard, Caddick and Tudor the entire first day of the Test.

ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests > ls -altr Old-Trafford-1990-K25.txt
-rw-r--r--. 1 root root 0 Feb 28 22:48 Old-Trafford-1990-K25.txt
ip-172-31-24-12.us-west-2.compute.internal: /Python-scripts/Memorable-Tests >






No comments:

Post a Comment