-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
27 lines (23 loc) · 852 Bytes
/
Copy pathmain.py
File metadata and controls
27 lines (23 loc) · 852 Bytes
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
import os
import re
class ItemRenamer:
def __init__(self, directory):
self.directory = directory
def list_items(self):
items = ""
for item in os.listdir(self.directory):
items+=f"sep{item}"
return "\n".join(items.split("sep"))
def rename_items(self, nametoremove):
self.nametoremove = nametoremove
matching_files = "Files renamed are: \n"
os.chdir(self.directory)
for file in os.listdir(self.directory):
if self.nametoremove in file:
filename = file
matching_files += f" sep{filename}"
newname = filename.replace(self.nametoremove, "")
os.rename(f"{self.directory}/{file}", newname)
else:
continue
return "\n".join(matching_files.split("sep"))