import os
import re

# Function to rename items (files and directories)
def rename_items(item_list):
    for item_name in item_list:
        # Replace spaces with hyphens
        new_name = item_name.replace(' ', '-')
        
        # Remove special characters except for periods and hyphens
        new_name = re.sub(r'[^\w\s.-]', '', new_name)
        
        # Rename the item
        if item_name != new_name:
            os.rename(item_name, new_name)

# Specify the directory path
directory_path = os.getcwd()

# Rename items in the directory (files and directories)
rename_items(os.listdir(directory_path))

# Create a list to store file names
file_list = []

# Iterate over the files in the directory and append their names to the list
for name in os.listdir(directory_path):
    if os.path.isfile(os.path.join(directory_path, name)):
        file_list.append(name)

# Specify the output text file name
output_file = 'file_list.txt'

# Write the file names to the output text file
with open(output_file, 'w', encoding='utf-8') as file:
    for name in file_list:
        file.write(name + '\n')

print(f'File list has been written to "{output_file}"')
