From 251f56c53f7b2438702e0b087f50d35d18343865 Mon Sep 17 00:00:00 2001 From: Icelain Date: Wed, 5 Oct 2022 19:08:37 +0530 Subject: [PATCH] made code quality improvements --- scripts/file_translater/trans.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/scripts/file_translater/trans.py b/scripts/file_translater/trans.py index 6ee43f4..5aaa67f 100644 --- a/scripts/file_translater/trans.py +++ b/scripts/file_translater/trans.py @@ -1,22 +1,23 @@ -# Imports import googletrans from googletrans import * - translator = googletrans.Translator() f1 = input("Enter source file (with .txt extension)") -f2 = open(f1, encoding="utf8") -data = f2.read() -# print(data) + +data = None + +# f2 is the file object which reads the source file +with open(f1, encoding="utf8") as f2: + data = f2.read() + f3 = input("Enter destination file (with .txt extension)") -f4 = open(f3, encoding="utf8", mode="w") -f2.close() -# input_text = input('Input Your Translation Text : ') -input_language = input('Input Your Translation Language : ') +# f4 is the file object which writes to the output file +with open(f3, encoding="utf8", mode="w") as f4: -translation = translator.translate(data, dest=input_language) -print(translation.text) -f4.write(translation.text) -f4.close() + input_language = input('Input Your Translation Language : ') + + translation = translator.translate(data, dest=input_language) + print(translation.text) + f4.write(translation.text)