Simple Programs On Data File Manipulations
Simple Programs On Data File Manipulations
Simple Programs On Data File Manipulations
File
Manipulation Description
Methods
fileno() Returns a number that represents the stream, from the operating system's perspective
seekable() Returns whether the file allows us to change the file position
a. Create a text file “intro.txt” in python and ask the user to write a single line of text by user
input.
Source Code:
def program1():
f = open("intro.txt","w")
f.write(text)
f.close()
program1()
Output:
Enter the text: Data Science is a subject which uses math and statistics, computer science algorithms and
research techniques to collect, analyze the data and make decisions for business purposes.
b. Create a text file “MyFile.txt” in python and ask the user to write separate 3 lines with
three input statements from the user.
Source Code:
def program2():
f = open("TypesOFData.txt","w")
newline = "\n"
f.write(line1)
f.write(newline)
f.write(line2)
f.write(newline)
f.write(line3)
f.write(newline)
f.close()
program2()
Output:
Enter the text:Data is of two types: Structured data and unstructured data
Enter the text:Structured data are organized and easy to work with.
c. Write a program to read the contents of both the files created in the above programs and
merge the contents into “merge.txt”. Avoid using the close() function to close the files.
Source Code:
def program3():
data1 = f1.read()
data2 = f2.read()
f3.write(data1)
f3.write(data2)
program3()
f3 = open("merge.txt","r")
f3.read()
Output:
'Data is of two types: Structured data and unstructured data\nStructured data are organized and easy to
work with.\nunstructured data is unorganized and is difficult to analyze it.\nData Science is a subject
which uses math and statistics, computer science algorithms and research techniques to collect, analyze
the data and make decisions for business purposes
d. Count the total number of upper case, lower case, and digits used in the text file
“merge.txt”.
Source Code:
def program4():
with open("merge.txt","r") as f1:
data=f1.read()
cnt_ucase =0
cnt_lcase=0
cnt_digits=0
for ch in data:
if ch.islower():
cnt_lcase+=1
if ch.isupper():
cnt_ucase+=1
if ch.isdigit():
cnt_digits+=1
print("Total Number of Upper Case letters are:",cnt_ucase)
print("Total Number of Lower Case letters are:",cnt_lcase)
print("Total Number of digits are:",cnt_digits)
program4()
Output: