# Python files demo # This function writes data into file
def fileWrite(): # fopen=open("sample.txt","w+") with open("sample.txt","w+") as fopen:# Trying to open a file using 'with' operator. This automatically closes the file. msg=raw_input('\nEnter the string you want to add to file:\t') fopen.write(msg) # fopen.close()
# This function reads data from a file
def fileRead(): fopen=open("sample.txt","r+") msg=fopen.read()# You can also specify the read(arg) to some numeric value to read till specific line size. print '\nCharacters read from the file: ',msg fopen.close()
# This function will append data to existing file
def fileAppend(): fopen=open("sample.txt","a+") msg=raw_input('\n Enter the string you want to append: ') fopen.write(msg) # Reading characters from file after appending str=fopen.read() print str
# This function splits words in a file & also counts # of words
def fileSplit(): with open("sample.txt","r+") as fobj: data=fobj.read() count=0 for i in data: word=data.split() print word print 'Word count is:',data.count(' ')+1# This function counts number of words in file
print '\n *** This program demonstrates various operations on Files****'
print '\n 1. Write to a file \n 2. Read from file \n 3. Append data to file \n 4. Word count in file'
option=input('Enter the option you want to perform:')
if option==1: print '***Write() will append the data to mentioned file (or) will create a new file ***' fileWrite()
elif option==2: print '\n *** Read() function will try to read data from existing file***' fileRead()
elif option==3: print '\n *** APpend() function will append data to mentioned file (or) create a new file if file doesn\'t exist' fileAppend()
elif option==4: print '\n ****Counting Number of words in file ****' fileSplit()
print '*** Select the operation you would like to perform *****'
print '\n 1. Prime Number between given interval \n 2. Armstrong Number \n 3. Even or Odd \n 4. Factorial Of number \n'
message='********* Welcome to python programming *****'
def test(message): print message return;
def prime_number(prime_range): for num in range(2,prime_range+1): for i in range(2, num): if (num%i==0): break else: print num return;
def armstrongNumber(num): temp=num; sum=0; while temp >1: rem=temp%10; sum+=rem**3 temp if sum==num: print (num,'is an Armstrong Number') else: print (num,'is not an Armstrong Number') return;
def factorial(num): fact=1 if(num < 1): print '\n Factorial doesnt exist for negative numbers' elif num==0: print '\n Factorial of 0 is 1' else: for i in range(1, num+1): fact=fact*i; print ('\n Factorial of ',num,' is: ',fact) return;
if option==1: prime_range=input('\n Enter the range till you want to find prime numbers:') prime_number(prime_range);
elif option==2: num=input('\n Enter the number to check if it is armstrong or not: ') armstrongNumber(num);
elif option==3: num=input('\n Enter the number to find even or odd: ') evenodd(num);
elif option==4: num=input('\n Enter the number to find factorial: ') factorial(num);
else: print '\n Invalid option'
Hi fnds, i hope u all liked the blog and if u feel that there is some thing that need to be changed/updated regarding the concept or some thing on blog please feel to post your valuable suggestions as comment so that i can correct it.
# Python being 'Indentation specific' the blog doesn't show indentation on python programs. Make sure you keep in mind when you go through. Thanks for viewing.