1

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

3
  • subprocess.Popen('echo %s|sudo -S %s' % (password, "date > " + file_path),stdin=subprocess.PIPE, shell=True) Commented Jan 7, 2018 at 9:02
  • This worked for me perfectly Thanks. Commented Jan 7, 2018 at 9:03
  • please up vote and mark as correct.... Commented Jan 7, 2018 at 10:35

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.