Showing posts with label File. Show all posts
Showing posts with label File. Show all posts

Program to write numbers 1 to 100 in a file

class File:

    def Create_file(self,fname):

        file = open(fname , 'w+')

        file.close()

        print("File Created as '{}'Successfully!\n".format(fname))


    def write_1__to_100(self,fname):

        try:

            fp = open(fname , 'w')

            for i in range(1,101):

                fp.write(str(i)+'\t')

            fp.close()

            print("\n Numbers written Successfully!")


        except FileNotFoundError:

            print('File not found')

if __name__ == '__main__':

    obj = File()

    k = 'C'

    while k=='C':

        ch = int(input("1.Create a File \n2.Write numbers 1 to 100 in file\nAny other key to Exit\n"))

        if ch==1:

            fname = input('Enter File name:')

            obj.Create_file(fname)

        elif ch==2:

            fname = input('Enter File name:')

            obj.write_1__to_100(fname)

        else:

            exit(0)

        k = input("Press 'C' to continue Other Key to exit:")

        print('\n')

else:

    pass

Reading files into Python

f = open("1.txt")
#create a text file which consists of names of students in class
# Printing file all the names
print("File contnet as in text file:",f.read())
f = open("1.txt")
# Printing file one by one letters
print("\nFile contnet one by one word fromtext file:")
next = f.read(1)
while next != "":
    print(next)
    next = f.read(1)

File operation for copying content of one file to another without for loop

#  Implementation without for loop
#  Copying content of 1.txt file to 2.txt after creation of file
#  Please provide the 1.txt file in the current folder as input and
#  check after runing the program in the same folder for 2.txt

I=open('1.txt',"r")
O=open('2.txt',"w")
C=I.readlines()
O.writelines(C)
I.close()
O.close()

File operation for copying content of one file to another

#  Copying content of 1.txt file to 2.txt after creation of file
#  Please provide the 1.txt file in the current folder as input and
#  check after runing the program in the same folder for 2.txt

I=open('1.txt',"r")
O=open('2.txt',"w")
for l in I.readlines():
    O.write(l)
I.close()
O.close()

Lisiting particular type of files in a directory

import os
Path = os.getcwd()
Names= os.listdir(Path)
h=raw_input('Enter the file type extension: ')
for n in Names:
      if h in n:
            print n

Enter the sum of n numbers using file

n=int(raw_input('Enter the n for sum of n numbers: '))
f=open('integers.txt','w')
for count in range(n+1):
      f.write(str(count)+"\n")
f.close()

f=open('integers.txt','r')
sum=0
for l in f:
      l=l.strip()
      number=int(l)
      sum+=number
print 'The sum is',sum

Writing and reading integers from a file

f=open('myfile.txt','w')
for count in  range(5):
      f.write(str(count))
f.close()
f=open('myfile.txt','r')
t=f.read()
print t

Writing and reading string from a file

f=open('myfile.txt','w')
f.write('\n Image Processing Research Group \n www.iprg.co.in \n')
f.close()
f=open('myfile.txt','r')
text=f.read()
print text