What is the best way to call following unix command in Python? cat file1.txt | tr -d '\r' > file2.txt
I tried following cases:
1.
cmd = "cat file1 | tr -d \'\r\'> file2"
args = shlex.split(cmd)
p = subprocess.Popen(args, shell=True)
I got cat: stdin: Input/output error
2.
f = open(file2, "w")
p = subprocess.call(args, stdout=f)
I got:
cat: |: No such file or directory
cat: tr: No such file or directory
cat: -d: No such file or directory
cat: \r: No such file or directory
3.
p = subprocess.Popen(args, stdout=subprocess.PIPE)
(out,err) = p.communicate()
print(out)
It works, but I do not know why when I use file.write(out)
instead of print(out)
, I get the same error as case 2.