Filehandling (Programs)
Filehandling (Programs)
Filehandling (Programs)
PROGRAMS
#Program in python to type text in a file till ~ is pressed as
rightmost character
f=open("d:\\a.txt","w")
s=''
print("Type text for file,press ~ as rightmost character for
exit/save contents")
while s!='~':
s=input()
f.write(s+"\n")
s=s[-1:]
f.close()
print("file contents saved")
#Python program to combine each line from first file with the
corresponding line in second file
f=open("d:\\a.txt","r")
g=open("d:\\b.txt","r")
for line1, line2 in zip(f,g):
print(line1+line2)
#Python Program to Copy the Contents of One File into
Another
f=open("d:\\a.txt","r")
g=open("d:\\c.txt","w")
for line in f:
g.write(line)
f.close()
g.close()
print("file contents copied")
#Python Program to Search for a word in text file and print
part of line
f=open("d:\\c.txt","r")
for line in f.readlines():
for part in line.split():
if "volatile" in part:
print(line)
f.close()
myint = 42
mystring = "Hello, world!"
mylist = ["dog", "cat", "lizard"]
mydict = { "name": "Bob", "job": "Astronaut" }
pickle.dump(myint, output_file)
pickle.dump(mystring, output_file)
pickle.dump(mylist, output_file)
pickle.dump(mydict, output_file)
output_file.close()
myint = pickle.load(input_file)
mystring = pickle.load(input_file)
mylist = pickle.load(input_file)
mydict = pickle.load(input_file)
input_file.close()