import os
import re

# Functions

def count_words(file):
    with open(file) as f:
        data = f.read()
        words = re.findall(r'\b\w+\b', data)
        return len(words)
    
def read_text(file):
    with open(file,'r') as f:
        print(f.read())
        
def removed_newLine(file):
    with open(file) as f:
        return ''.join([line.rstrip('\n') for line in f])
    
def full_cap(file):
    with open(file) as f:
        return ''.join([line.upper() for line in f])
    
def file_length(file):
    with open(file) as f:
        return sum(1 for _ in f)
    
def Longest_word(file):
    with open(file) as f:
        words = f.read().split()
        return [w for w in words if len(w) == len(max(words, key=len))]
    
def Add_new_line(file):
    text = input("Enter Your Text: \n")
    
    with open(file, "a") as f:
        f.write(f"{text}\n")
        
    print("\n.... Output of nwe line after adding new line... \n")
    with open(file, "r") as f:
        print(f.read())

text_file = input("Enter FileName: \n")


if os.path.exists(text_file):
    # print("File Exists")
    while True:
        query = input("\nEnter Q to Quit\nEnter R to reed text\nEnter C to Count Word\nEnter RN to Remove new line\nEnter FC to view Full Capital\nEnter FL to view File lenght\nEnter LW to view Longest Word\nEnter A to Add new line\n")
        
        if(query == "Q"):
            break
        elif(query == "R"):
            print(read_text(text_file))
        elif(query == "C"):
            print(f"Total Number Of Words is {count_words(text_file)}")
        elif(query == "RN"):
            print(removed_newLine(text_file))
        elif(query == "FC"):
            print(full_cap(text_file))
        elif(query == "FL"):
            print(f"Total Length of row is: {file_length(text_file)}")
        elif(query == "LW"):
            print(f"The Longest Word IS: {Longest_word(text_file)}")
        elif(query == "A"):
            print(Add_new_line(text_file))
    
else:
    print("File Not Found")