Saturday, May 22, 2021

Create a file, add contents and display the file

Script

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

bengalurufile_create = open("/Python-scripts/Memorable-Tests/Bangalore-2017-K20.txt","w+")
bengalurufile_create.write("India beat Australia by 75 runs after trailing by 87 runs in the first innings.\n")
bengalurufile_create.write("Cheteshwar Pujara and Ajinkya Rahane revived the situation for India in the second innings.\n")
bengalurufile_create.write("Steven Smith got into trouble with the umpires when he looked up to the pavilion before reviewing the LBW decision.\n")
bengalurufile_create.close()

smithlbw = open('/Python-scripts/Memorable-Tests/Bangalore-2017-K20.txt','r')
print()
print("Filename: ",smithlbw.name)
print("Mode of opening the file: ",smithlbw.mode)
print()
print("------------------------------------------------------------------------------")
print("\n")
print("The file contents are:\n")
print(smithlbw.read())
smithlbw.close()


Before executing the script

kirands-vm.company.com: /Python-scripts >
kirands-vm.company.com: /Python-scripts > ls -altr /Python-scripts/Memorable-Tests/Bangalore*.*
ls: cannot access /Python-scripts/Memorable-Tests/Bangalore*.*: No such file or directory
kirands-vm.company.com: /Python-scripts >


Execution

Filename:  /Python-scripts/Memorable-Tests/Bangalore-2017-K20.txt
Mode of opening the file:  r

------------------------------------------------------------------------------


The file contents are:

India beat Australia by 75 runs after trailing by 87 runs in the first innings.
Cheteshwar Pujara and Ajinkya Rahane revived the situation for India in the second innings.
Steven Smith got into trouble with the umpires when he looked up to the pavilion before reviewing the LBW decision.


After executing the script

kirands-vm.company.com: /Python-scripts >
kirands-vm.company.com: /Python-scripts > cat /Python-scripts/Memorable-Tests/Bangalore-2017-K20.txt
India beat Australia by 75 runs after trailing by 87 runs in the first innings.
Cheteshwar Pujara and Ajinkya Rahane revived the situation for India in the second innings.
Steven Smith got into trouble with the umpires when he looked up to the pavilion before reviewing the LBW decision.
kirands-vm.company.com: /Python-scripts >




No comments:

Post a Comment