0% found this document useful (0 votes)
32 views

Simple Programs On Data File Manipulations

The document describes 4 programs that perform various file manipulation tasks in Python: 1. Asks the user to input text and writes it to a file called "intro.txt". 2. Asks the user to input 3 lines of text and writes them to a file called "MyFile.txt". 3. Reads the contents of the two files created in the previous programs and merges them into a new file called "merge.txt". 4. Counts the total number of uppercase letters, lowercase letters, and digits in the merged "merge.txt" file.

Uploaded by

Venkata Naresh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Simple Programs On Data File Manipulations

The document describes 4 programs that perform various file manipulation tasks in Python: 1. Asks the user to input text and writes it to a file called "intro.txt". 2. Asks the user to input 3 lines of text and writes them to a file called "MyFile.txt". 3. Reads the contents of the two files created in the previous programs and merges them into a new file called "merge.txt". 4. Counts the total number of uppercase letters, lowercase letters, and digits in the merged "merge.txt" file.

Uploaded by

Venkata Naresh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1. Simple programs on Data file manipulations.

File
Manipulation Description
Methods

close() Closes the file

detach() Returns the separated raw stream from the buffer

fileno() Returns a number that represents the stream, from the operating system's perspective

flush() Flushes the internal buffer

isatty() Returns whether the file stream is interactive or not

read() Returns the file content

readable() Returns whether the file stream can be read or not

readline() Returns one line from the file

readlines() Returns a list of lines from the file

seek() Change the file position

seekable() Returns whether the file allows us to change the file position

tell() Returns the current file position

truncate() Re-sizes the file to a specified size

writable() Returns whether the file can be written to or not

write() Writes the specified string to the file

writelines() Writes a list of strings to the file

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")

text = input("Enter the text: ")

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")

line1 = input("Enter the text:")

line2 = input("Enter the text:")

line3 = input("Enter the text:")

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.

Enter the text:unstructured data is unorganized and is difficult to analyze it.

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():

with open("MyFile.txt","r") as f1:

data1 = f1.read()

with open("intro.txt","r") as f2:

data2 = f2.read()

with open("merge.txt", "w") as f3:

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:

Total Number of Upper Case letters are: 4


Total Number of Lower Case letters are: 289
Total Number of digits are: 0

You might also like