# Python Regular Expression - Example Program

import re

str = "This python regular expression tutorial on technoexam.com"

matchObject = re.match( r'(.*) python (.*?) .*', str, re.M|re.I)

if matchObject:
	print("matchObject.group():", matchObject.group())
	print("matchObject.group(1):", matchObject.group(1))
	print("matchObject.group(2):", matchObject.group(2))
else:
	print("Sorry..!!..No match found..!!")
Output :
matchObject.group(): This python regular expression tutorial on technoexam.com
matchObject.group(1): This
matchObject.group(2): regular
# Python Program to Get an IP Address of your PC.
		
import socket
while True:
	print("Want to get IP Address ? (y/n): ")
	check = input()
	if check == 'n':
		break
	else:
		print("Your IP Address is: ")
		print(socket.gethostbyname(socket.gethostname()))
		print()
Output :
Want to get IP Address ? (y/n):
y
Your IP Address is:
192.168.8.100

Want to get IP Address ? (y/n):

# Python Program code to Delete File
		
import os
while True:
	print("Enter 'x' for exit.")
	filename = input("Enter name of file to delete: ")
	if filename == 'x':
		break
	else:
		print("Removing the file....")
		os.remove(filename)
		print("File,",filename,"successfully deleted!!\n")
Output :
Enter ‘x’ for exit.
Enter name of file to delete: delete.txt
Removing the file….
File, delete.txt successfully deleted!!

Enter ‘x’ for exit.
Enter name of file to delete:

# Python Programming Code to Merge Two Files

# Following python program ask from user to enter first 
# and second file name, then to create a file to enter 
# or place the merged content of first and second file 
# into this file as shown here in this program:

import shutil
while True:
	print("Enter 'x' for exit.")
	filename1 = input("Enter first file name: ")
	filename2 = input("Enter second file name: ")
	if filename1 == 'x':
		break
	else:
		filename3 = input("Create a new file to merge content of two file inside this file: ")
		print()
		print("Merging the content of two file in",filename3)
		with open(filename3, "wb") as wfd:
			for f in [filename1, filename2]:
				with open(f, "rb") as fd:
					shutil.copyfileobj(fd, wfd, 1024*1024*10)
		print("Content merged successfully.!")
		print("Want to see ? (y/n): ")
		check = input()
		if check == 'n':
			break
		else:
			c = open(filename3, "r")
			print(c.read())
			c.close()
			print()
		print()
Output :
Enter ‘x’ for exit.
Enter first file name: merge1.txt
Enter second file name: merge2.txt
Create a new file to merge content of two file inside this file: mergedfile.txt

Merging the content of two file in mergedfile.txt
Content merged successfully.!
Want to see ? (y/n):
y
Welcome to Technowell We are happy to provide excellent services.
we provide software solutions

Enter ‘x’ for exit.
Enter first file name:

# Python Programming Code to Copy Files.
# ( You have to create two .txt file one with giving input lines 
# and  other to copy that data.you have to enter file names for output.)

from shutil import copyfile
while True:
	print("Enter 'x' for exit.")
	sourcefile = input("Enter source file name: ")
	destinationfile = input("Enter destination file name: ")
	if sourcefile == 'x':
		break
	else:
		copyfile(sourcefile, destinationfile)
		print("File copied successfully!")
		print("Want to display the content ? (y/n): ")
		check = input()
		if check == 'n':
			break
		else:
			c = open(destinationfile, "r")
			print(c.read())
			c.close()
			print()
		print()
Output :
Enter ‘x’ for exit.
Enter source file name: “input.txt”
Enter destination file name: “output.txt”
File copied successfully!
Want to display the content ? (y/n):
“y”
Sample input goes here..
Any data you can add here..

Enter ‘x’ for exit.
Enter source file name:

# Read from File in Python
# Use read() function to read the content present inside the file.
# Python File IO - Python File Handling - Example Program

c = open("myfile.txt", 'r');

print("File, myfile.txt, opened successfully..!!");
print("The file contains :\n");
print(c.read());

c.close();

print("The file closed successfully..!!");
Output :
File, myfile.txt, opened successfully..!!
The file contains :

this is python file io tutorial
this is python file io example
this is python file handling tutorial
this is python file handling example

The file closed successfully..!!

# Write to File in Python
# Python File IO to Python File Handling - Example Program
# (create new blank text file with name 'myfile.txt' in same folder of python.)

c = open("myfile.txt", 'w')
print("File created, successfully..!!");
print("writing some content inside the file....\n");

c.write("this is python file io tutorial\n");
c.write("this is python file io example\n");
c.write("this is python file handling tutorial\n");
c.write("this is python file handling example\n");

c.close()

print("File successfully closed..!!"); 
# now you can open myfile.txt 
#you will get file having given strings in it . 

#this is python file io tutorial
#this is python file io example
#this is python file handling tutorial
#this is python file handling example
Output :
File created, successfully..!!
writing some content inside the file….

File successfully closed..!!