1. Write a program to write all multiple of 7 from 10 to 100 in to a file

f=open(‘file1.txt’,’w’)

for i in range(14,100,7):

 f.write(str(i)+ ‘n’)

f.close()

2.  Write all elements in the list to a file

a=[‘hellon’,’hown’,’aren’,’youn’]

f=open(‘file1.txt’,’w’)

a=[‘hellon’,’hown’,’aren’,’youn’]

f.writelines(a) #write all the elements in list into file

f.close()

print(f.closed)        #Prints whether the file is closed or not

3. Write a program to count the number of words in a text file

num_words = 0

f=open(‘input.txt’,’r’)

for line in f:

words = line.split()

num_words += len(words)

print(“Number of words:”,num_words)

f.close()

 

4. Write a program to count the occurrences of a word in a text file

f=open(‘input.txt’,’r’)
word=input(“Enter word to be searched:”)
k = 0
for line in f:

words = line.split()

for i in words:

if(i==word):

k=k+1

print(“Occurrences of the word:”,k)

f.close()

5. Write a program to copy the contents of one file to another file

infile=open(‘input.txt’,’r’)
outfile=open(‘out.txt’,’w’)
for line in infile:

outfile.write(line)

infile.close()
outfile.close()

6.  Write a  Program to Read a File and Capitalize the First Letter of Every Word in the File and print it on the screen

f=open(‘out.txt’,’r’)

for line in f:

l=line.title()

print(l)

7. Write a program to take each item from the list and check whether we are able to ping to the system or not

 

import os,time

#list of nodename/ipaddress

nodes=[“www.google.com”,”www.gmail.com”,”serverhyd1″,”129.135.162.3″,”127.0.0.1″]

#function that returns on or off

def isup(host):

pingstring= “ping -n 1 {}”.format(host)

status=os.system(pingstring)

if not status:

return host + ” is up”

else:

return host + ” is down”

while True:

for node in nodes:

print(isup(node))

time.sleep(1) #sleeps for a second

8. Write a program to open and write to a file if the file exists else create a new file  and write to the file

 

import os,sys

#Opening a file if exists else creates a new file

fd = os.open( “c:\Users\new2.txt”, os.O_RDWR|os.O_CREAT )

#Write string to the file

os.write(fd, “Writing in to test file”)

#Closing the opened file

os.close(fd)

print “Closed the file successfully”

9.  Write a program print all the files which are more than 100 bytes

import os

import glob

import time

mydir=”C:/Users/Exercises”

mysize=100

os.chdir(mydir)

myfiles=glob.glob(“*.py”)

for file in myfiles:

fsize=os.stat(file).st_size

if  fsize > mysize:

print(file,fsize)

10.  Write a program to get the files that were created in the last 4 hours

import glob,os,time

filedir=(“C:/Users/Exercises”)

os.chdir(filedir)

pyfile=glob.glob(‘*.py’)

totalsize=0

for file in pyfile:

if time.time()-os.stat(file).st_ctime < 4 *60 *60:

totalsize+=os.stat(file).st_size

print (file,os.stat(file).st_size)

print (totalsize)

11.  Write a function input_stats that accepts a file name as a parameter and that reports the longest line in the file.

example input file, input3.txt:

Smile it makes you beautiful.

Do that every morning and you’ll start to see a big difference in your life.

expected output:76

def input_stats(filename):

f = open(filename,’r’)

longest = “”

for line in f:

#print (len(longest))

if len(line) > len(longest):

longest = line

print(“Longest line =”, len(longest))

print(longest)

input_stats(“input3.txt”)

12. Create a dictionary from text file

Sample File :

car=mode of transport

bp=bloodpressure

lvl=cholestrol

stop=busstop

railways=irctc

city=mumbai

Write a program to read from file, split the line with ‘=’ and store it as words and meanings in to the dictionary

 

f=open(‘words.txt’,’r’)

words={}

for line in f:

pos=line.index(‘=’)             #find the index of =

word=line[:pos]                 #gets the word

#print (word)

meaning=line[pos+1:].rstrip()   #gets the meaning

words[word]=meaning             #adds the word and meaning to the dictionary

print (words)

f.close()

13. Previous program will not work if there are comments and blanklines, write a program

so that it eliminates blank lines and comments and adds the other words and meanings to the dictionary

 

f=open(‘words1.txt’,’r’)

words={}

for line in f:

line=line.strip()

if line and not line.startswith(‘#’):

#if ‘=’ in line:

pos=line.index(‘=’)#find the index of = #prints the word

word=line[:pos]  #

meaning=line[pos+1:].rstrip()#meaning

words[word]=meaning

print (words)

f.close()

14. Write a program to print the details of dirname, subdir and files

 

import os,time

for (dirname, subdir, files) in os.walk(‘c:/pythonEx’):

for myfile in files:

filename=os.path.join(dirname,myfile)

print(filename)