Untitled Document
File Objects - Reading and Writing Files
1. Program - Opening file in read mode Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****  
       # In this chapter we will open files in different mode ; if you donot specify any mode then default mode is readonly 
       # we have test.txt file current directory lets perform some file operations on it   
         
       # Program - Opening file in read mode
       # IMP - when we open file with below method we explicitly need to close the file 
       
       f = open('test.txt', 'r')
       print(f.mode)
       print(f.name)
       f.close()

       
2. Program - Opening & Reading files using conext managers Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - Opening & Reading files using conext managers
       # For file objects we will use context managers instead of using above approach
       # the conext managers are easily managable and we don't need to explicitly close the file
       
       with open('test.txt', 'r') as f:
       	f_contents = f.read()
       	print(f_contents)
       
       # IMP - read() method flush entire file contents on terminal ; 
       # Now if you have file have large contents then it is not advisable to use above approach
       
       # Another way of printing lines from file 
       
       with open('test.txt', 'r') as f:
       	f_contents = f.readline()
       	print(f_contents)
       
       # The readline() function will print only one line from a file 
       with open('test.txt', 'r') as f:
       	f_contents = f.readline()
	   	print(f_contents,end='')
	   	f_contents = f.readline()
	   	print(f_contents,end='')
       
       # The above 4 lines will print next 2 lines on terminal ; but again this is not the way to print entire file on termial 
       
       # readlines() - This function will print all the file contents in list 
       
       with open('test.txt', 'r') as f:
       	f_contents = f.readlines()
       	print(f_contents)

       
3. Program - Printing file contents using for loop Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - Printing file contents using for loop

       with open('test.txt', 'r') as f:
       	for line in f:
       		print(line, end='')

       
4. Program - Printing file contents using charcater count Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - Printing file contents using charcater count
       # Below example will print 50 characters from file 
       
       with open('test.txt', 'r') as f:
       	f_contents = f.read(50)
       		print(f_contents, end='')

       
5. Printing file contetnts using charcater size using while loop Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Printing file contetnts using charcater size using while loop
       
       with open('test.txt', 'r') as f:
       	size_to_read = 5            
       	f_contents = f.read(size_to_read)
                    
       	while len(f_contents) > 0:
       		print(f_contents, end='')
       #OR
       		print(f_contents, end='--')
       		f_contents = f.read(size_to_read)
       
       
6. seek() method Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # seek() method
       # seek() method is used to change or move the cursor position of file
       # You can move the cursor position to any position ; seek(0) will take cursor position to the begining of file 
       
       with open('test.txt', 'r') as f:
       	size_to_read = 30            
       	f_contents = f.read(size_to_read)
       	print(f_contents, end='')
       
       	f.seek(0)
       	f_contents = f.read(size_to_read)
       	print(f_contents, end='')
 
       
7. Creating empty file using write mode w Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Creating empty file using write mode w
       
       # below example we will create test2.txt file  
       
       with open('test2.txt','w') as f:
       	pass            
 
       
8. writing text to file Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # writing text to file
       # below example will write some text to file 
      
       with open('test2.txt','w') as f:
       	f.write('Test -- Sample text')       
       
       
9. Copying file contents from one file to another Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Copying file contents from one file to another
              
       with open('test.txt', 'r') as rf:
       	with open('test_copy.txt', 'w') as wf:
       		for line in rf:
       			wf.write(line)            
       
       
10. Copying image file from one file to another Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Copying image file from one file to another 
       
       with open('11.jpg', 'rb') as rf:
       	with open('11_copy.jpg','wb') as wf :
       		for line in rf:
       			wf.write(line)

       
Untitled Document