command = "cat /home/norman/khconf/second.txt | sort | uniq > /home/norman/khconf/third.txt".split() subprocess.call(command)When you open a file with 'w' flag for writing it overwrites the previous content. Use 'a' to append.
https://docs.python.org/3/library/functions.html#open