Practical 3 - File Handling

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Practical 3:

Implement Python Program to demonstrate File handling


- Python program to append data to existing file and then display the entire file.
- Python program to count number of lines, words and characters in a file.
- Python program to display file available in current directory
- Regular Expression

P1: Simple File Program

# Create a new file and write data to it

with open(file_name, 'w') as file:

file.write(data)

print(f"File '{file_name}' created and data written successfully.")

# Read data from an existing file

with open(file_name, 'r') as file:

content = file.read()

print(f"Content of file '{file_name}':\n{content}")

# Example Usage

file_name = "example_file.txt"

data_to_write = "This is some data to write to the file."

# Create and write to the file

create_and_write_to_file(file_name, data_to_write)

# Read from the file

read_from_file(file_name)
P2 : Python program to append data to existing file and then display the entire file.

# Append data to the existing file


with open(file_name, 'a') as file:
file.write(data_to_append + '\n')

# Display the entire file


with open(file_name, 'r') as file:
content = file.read()
print("Entire File Content:")
print(content)

Python program to count number of lines, words and characters in a file.


with open(file_name, 'r') as file:

content = file.read()

# Counting lines, words, and characters

lines = content.count('\n') + 1

words = len(content.split())

characters = len(content)

print(f"Number of lines: {lines}")

print(f"Number of words: {words}")

print(f"Number of characters: {characters}")

3. Python program to display file available in current directory

import os

# Get the current directory


current_directory = os.getcwd()

os.listdir(current_directory

# Regular expressions

#Regular expression

# Need to import re library

import re

# Demo of finaall with different options

# Demo1: find mentioned characters

txt = "hello planet"

#Search for a sequence that starts with "he", followed by two (any) characters, and an "o":

x = re.findall("he..o", txt)

print(x)

#Demo2: Find digits

txt = "That will be 59 dollars"

#Find all digit characters:

x = re.findall("\d", txt)

print(x)

# Demo 3: Return a list containing every occurrence of "ai":

txt = "The rain in Spain"

x = re.findall("ai", txt)

print(x)

# Demo4: Zero or more occurrence


txt = "hello planet"

#Check if the string starts with 'hello':

x = re.findall("^hello", txt)

if x:

print("Yes, the string starts with 'hello'")

else:

print("No match")

# Demo5: ending match

#Check if the string ends with 'planet':

x = re.findall("planet$", txt)

if x:

print("Yes, the string ends with 'planet'")

else:

print("No match")

# Demo6:

#Search for a sequence that starts with "he", followed by 1 or more (any) characters, and an "o":

x = re.findall("he.+o", txt)

print(x)

# Demo6 :Ends with

#Check if the string ends with 'planet':

x = re.findall("planet$", txt)

if x:

print("Yes, the string ends with 'planet'")

else:

print("No match")
import re

# Example 1: Matching a pattern in a string

pattern_1 = r'\b\w+@\w+\.\w+\b'

text_1 = "Email addresses: user1@example.com, user2@gmail.com"

matches_1 = re.findall(pattern_1, text_1)

print("Email addresses:", matches_1)

# Example 2: Extracting phone numbers

pattern_2 = r'\b\d{3}-\d{3}-\d{4}\b'

text_2 = "Phone numbers: 123-456-7890, 987-654-3210"

matches_2 = re.findall(pattern_2, text_2)

print("Phone numbers:", matches_2)

You might also like