While Chintan's answer is straightforward, it requires all your code be in an extra indentation block. The best way to do this without extra indent block is
import sys sys.stdin = open('input.txt', 'r', encoding='utf-8')