Professional Documents
Culture Documents
Practical 3 - File Handling
Practical 3 - File Handling
Practical 3 - File Handling
file.write(data)
content = file.read()
# Example Usage
file_name = "example_file.txt"
create_and_write_to_file(file_name, data_to_write)
read_from_file(file_name)
P2 : Python program to append data to existing file and then display the entire file.
content = file.read()
lines = content.count('\n') + 1
words = len(content.split())
characters = len(content)
import os
os.listdir(current_directory
# Regular expressions
#Regular expression
import re
#Search for a sequence that starts with "he", followed by two (any) characters, and an "o":
x = re.findall("he..o", txt)
print(x)
x = re.findall("\d", txt)
print(x)
x = re.findall("ai", txt)
print(x)
x = re.findall("^hello", txt)
if x:
else:
print("No match")
x = re.findall("planet$", txt)
if x:
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)
x = re.findall("planet$", txt)
if x:
else:
print("No match")
import re
pattern_1 = r'\b\w+@\w+\.\w+\b'
pattern_2 = r'\b\d{3}-\d{3}-\d{4}\b'