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

1 comment:

Anonymous said...

with open("write1-100.txt", "wt", encoding="UTF-8") as f:
f.write("\t".join(map(str, range(10))))