Showing posts with label Files. Show all posts
Showing posts with label Files. Show all posts

PYTHON ASSIGNMENT 9: FILES




Read from console and write to file

Read from file and display in console

Display file content with line number

Display first N lines of a file

Display nth line of a file

Display from nth line to mth line of a file

Copy file

Copy first N lines from a file to new file

Copy from nth line to mth line of a file to new file

Merge two files

Count number of characters, words and lines in a file

Search if a word exists in a file.

Count number of times a word exists in a file

Search if a word exists in a file. Display all lines in which the word exists.

Delete a word in a file

Replace a word in a file

Write a program that creates a dictionary of the frequency of all words in a file. Remove noise words like “the”, “and”, and so on. Display the three most frequently occurring words.

Write a program that creates a dictionary of the frequency of all the characters in a file. Display five most frequently occurring characters.

Given a file containing customer names and their phone numbers, write a program to find the telephone number of a given customer.

Compare two files and print the first line where they differ.




PYTHON ASSIGNMENT 7: MISCELLANEOUS PROGRAMS

Operators and Control Structures

Swap two numbers

Circulate the values of n variables

Find GCD of two numbers

Distance between two points

Square root of a number using Newton’s method 

Exponentiation of a number 

First N prime numbers



List

Maximum in a given list of numbers

Sum of elements in a list

Matrix multiplication


Search

Linear search

Binary search


Sort

Insertion sort

Selection sort

Merge sort


Dictionaries : Histograms

Read a string and find the 5 most frequent characters

Read a string and find the 3 most frequent words


File operations:

File copy

Merge two files

Read a file name from command line and count the number of lines, words and characters in the file.

Read a file and create a dictionary for the frequency of words in the file

Read a file and create a dictionary for the frequency of characters in the file.

File handling 18: Files and dictionary, Histogram, character frequency

# Write a program that creates a dictionary
# of the frequency of all characters in a file.
# display five most frequently occuring characters

# Files, Dictionary and Command line arguments
# Histogram - Character frequency

import sys
import operator

fname = sys.argv[1]
fs = open(fname, "r")

Char_freq = {}

while(True):
    Line = fs.readline().strip().lower()
   
    if not Line:
        break
   
    for ch in Line:
        if (ch.isalpha()):
            if ch in Char_freq:
                Char_freq[ch] += 1
            else:
                Char_freq[ch] = 1
           
for ch in Char_freq:
    print(ch, Char_freq[ch])

List1 = sorted(Char_freq.items(), key=operator.itemgetter(1), reverse=True)

print("Five most frequently occuring characters :")
for k,v in List1[0:5]:
    print(k,v)

fs.close()

File Handling 19: Name and phone number

# Given a file containing customer names and their phone numbers, 
# write a program to find the telephone number of a given customer.

# Files: Name and phone number
import sys

fname = sys.argv[1]
fs = open(fname,"r")
Flag = False

Name = input("Enter name :")
Name = Name.strip().lower()

while(True):
    line = fs.readline()
   
    if not line:
        break
   
    L = line.split(" ")
    if(L[0].strip().lower() == Name):
        Flag = True
        print("Name =", Name.capitalize())
        print("Phone number =", L[1])
        break

if(Flag==False):
    print(Name, "- Name not found in directory")
fs.close()

File handling 20: Compare two files and print the line where they differ

# Files: Difference between files

import sys

try:
    fname1 = sys.argv[1]
    fname2 = sys.argv[2]

    fs1 = open(fname1,"r")
    fs2 = open(fname2,"r")
    Flag = False
  
    L1 = fs1.readlines()
    L2 = fs2.readlines()

    nlines = min(len(L1), len(L2))

    for idx in range(0, nlines, 1):
        if (L1[idx] != L2[idx]):
            print("Files differ at line number", idx+1)
            Flag = True
            break
          
    if(Flag == False):
        if(len(L1)==len(L2)):
            print("Files are the same. They do not differ")
        else:
            print("Files differ at line number", nlines+1)
except:
    print("Error")
  
finally:  
    fs1.close()
    fs2.close()



File Handling 17: Files and Dictionary: Histogram, Word Frequency, remove noise words

# Write a program that creates a dictionary
# of the frequency of all words in a file.

# Files, Dictionary and Command line arguments
# Histogram - Word frequency

import sys

fname = sys.argv[1]
fs = open(fname, "r")

Word_freq = {}

while(True):
new_Line = ""
Line = fs.readline().strip().lower()

if not Line:
break

for ch in Line:
if (ch.isalpha() or ch.isspace()):
new_Line+=ch
L = new_Line.split(" ")

for wd in L:
if wd in Word_freq:
Word_freq[wd] += 1
else:
Word_freq[wd] = 1

for ele in Word_freq:
print(ele.ljust(15), Word_freq[ele])

print("Number of unique words in file = ", len(Word_freq))

fs.close()




# Write a program that creates a dictionary
# of the frequency of all words in a file.
# Remove noise words in file like a, an, the etc

# Files, Dictionary and Command line arguments
# Histogram - Word frequency

import sys

fname = sys.argv[1]
fs = open(fname, "r")

Word_freq = {}

Noise_words = ['an', 'as', 'of', 'it','by',  'to', 'so', 'do', 'be', 'up', 'on', 'ie', 'its', 'are', 'all', 'has', 'can', 'how', 'end', 'any', 'may', 'for', 'will', 'use', 'one', 'two', 'the', 'also', 'have', 'this', 'that', 'what', 'where', 'when', 'then', 'those', 'from', 'once', 'more', 'most' ]

while(True):
new_Line = ""
Line = fs.readline().strip().lower()

if not Line:
break

for ch in Line:
if (ch.isalpha() or ch.isspace()):
new_Line+=ch
L = new_Line.split(" ")

for wd in L:
if (len(wd)<=1 or wd in Noise_words):
continue

if wd in Word_freq:
Word_freq[wd] += 1
else:
Word_freq[wd] = 1

for ele in Word_freq:
print(ele.ljust(15), Word_freq[ele])

print("Number of unique words in file = ", len(Word_freq))

fs.close()

File Handling 3: Display file content with line number

# Display file content with line number
# Use command line arguments

import sys

fname = sys.argv[1]

fs = open(fname, "r")
Line_num = 1

print("File:", fs.name)

while(True):
Line = fs.readline()

if not Line:
break

print(Line_num, Line)
Line_num += 1

fs.close()


# Display file content with line number
# Use command line arguments

import sys

fname = sys.argv[1]
fs = open(fname, "r")

print("File:", fs.name)

L = fs.readlines()
for Line in L:
print(L.index(Line)+1, Line)

fs.close()