-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_every_nthCharacter.py
More file actions
56 lines (44 loc) · 1.35 KB
/
remove_every_nthCharacter.py
File metadata and controls
56 lines (44 loc) · 1.35 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Author : Ashutosh Kumar
PRN : 19030142009
Assignment - To remove every nth letter from a file and copy the rest of the data into second file
"""
new_file=""
fp=""
# opening file in read mode
try:
file_name=input("Enter File Name")
fp = open(file_name, 'r')
# creating new file for new data
new_file = open("newdata.txt", 'w')
# character which you can want to remove
skipped = int(input("Enter Character number to skip"))
copy_of_skip = skipped - 1 # calculating index to remove character
# extracting each line
for line in fp:
# providing index to be skipped
skipped = copy_of_skip
for char in line:
# comparing each character
if line.index(char) is not skipped:
new_file.write(char)
else:
print(char, skipped)
# calculating next index
skipped = skipped + (copy_of_skip + 1)
except TypeError:
print("Input given is incorrect")
except FileNotFoundError:
print("File not Found ")
except FileExistsError:
print("File Already Exists")
except PermissionError:
print("Permissison to open file is not granted ")
except IsADirectoryError:
print("It is a directory")
finally:
try:
new_file.close();
fp.close()
except AttributeError:
print("File not opened ")