I am trying to run this using python3
system('echo %s|sudo -S %s' % (password, "date > " + file_path))
but getting this as error
sh: 2: Syntax error: "|" unexpected
A possibly unrelated problem is that you aren't properly quoting the input and output the variables in the command.
system('echo "%s"|sudo -S "%s"' % (password, "date > " + file_path))
However, for all legal filenames as filenames. I would recommend using the subprocess module instead of os.system, leaving the shell out of the process altogether:
subprocess.Popen('echo %s|sudo -S %s' % (password, "date > " + file_path), stdout=subprocess.PIPE)
subprocess.Popen('echo %s|sudo -S %s' % (password, "date > " + file_path),stdin=subprocess.PIPE, shell=True)