-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmbed_string_in_file.py
29 lines (22 loc) · 1.01 KB
/
Embed_string_in_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def embed(fileName, startIndex, s):
try:
# Open the file in read mode and read all contents
with open(fileName, 'r') as filename:
lines = filename.read()
# Check if the startIndex is within valid range
if startIndex > len(lines) or startIndex < 0:
raise IndexError(f"The start index {startIndex} is out of range.")
# Slice and embed the string 's' at the specified startIndex
modified_lines = lines[:startIndex] + s + lines[startIndex:]
# Write the modified content to a new output file
with open('output7.txt', 'w') as outfile:
outfile.write(modified_lines)
# Handle invalid index
except IndexError as e:
print(f"Error: {e}")
# Handle file not found errors
except FileNotFoundError as e:
print(f"Error: {e}. The file '{fileName}' was not found.")
# Example Usage
embed('input.txt', 0, 'Good Morning ')
# The example embeds 'Good Morning ' at the start (index 0) of 'input.txt'.