import os
def convert_rtf_to_txt(directory):
files = os.listdir(directory)
for file in files:
if os.path.isfile(os.path.join(directory, file)):
filename, extension = os.path.splitext(file)
if extension.lower() == ".rtf":
rtf_file = open(os.path.join(directory, file), "r")
rtf_content = rtf_file.read()
rtf_file.close()
new_name = f"{filename}.txt"
txt_file = open(os.path.join(directory, new_name), "w")
txt_file.write(rtf_content)
txt_file.close()
os.remove(os.path.join(directory, file))
print("RTF to TXT conversion complete.")
directory_path = "D:\\rtf files"
convert_rtf_to_txt(directory_path)
This code converts all RTF files in the specified directory to TXT format by reading the content of each RTF file, creating a corresponding TXT file with the same content, and finally removing the original RTF files.