Save results in the files with same name as input files
Question: I am working on a python code which is as follows:

    
    
    import os
    count = 0
    for doc in os.listdir('/home/krupa/Krupa/Mirellas_Image_Annotation_Data/Test/Html_Files/Texts'):
       if doc.endswith(".txt"):
          with open(doc, 'r') as f:
              single_line = ''.join([line for line in f])
              single_space = ' '.join(single_line.split())
    
          with open(doc.format(count) , "w") as doc:
            doc.write(single_space)
        count += 1
    else:
        continue
    

Here I want to write the output in the same file name but with different
extension (say .key). How do I do it? Please help. Thanks in advance


Answer: 
    import os
    count = 0
    for doc in os.listdir('/home/krupa/Krupa/Mirellas_Image_Annotation_Data/Test/Html_Files/Texts'):
        doc1 = "doc_path" + doc
        doc2 = "new_path_where_new_file_with_new_ext_needs_to_be_saved" + doc1
        if doc1.endswith(".txt"):
        with open(doc, 'r') as f:
               single_line = ''.join([line for line in f])
               single_space = ' '.join(single_line.split())
    
               with open(doc2.format(count) , "w") as doc2:
                   doc2.write(single_space)
               count += 1
    else:
    continue
    

