Friday, May 3, 2024
7
rated 0 times [  7] [ 0]  / answers: 1 / hits: 1382  / 2 Years ago, sat, december 18, 2021, 9:21:13

I have a Python script, EulerianCycle.py, and an input file, euleriancycle.txt.


I am able to get the correct results by doing py EulerianCycle euleriancycle.txt > cat euleriancycleout.txt into the current folder (py is an alias for python3).


However, I have another folder in this current one called outputs, to which I want all my output files be directed.


I've tried py EulerianCycle.py euleriancycle.txt | cd outputs/ | cat > euleriancycleout.txt


And py EulerianCycle.py euleriancycle.txt | cat >cd outputs/euleriancycleout.txt


which gives me the broken pipe error.


More From » command-line

 Answers
7

If py EulerianCycle.py euleriancycle.txt writes to the standard output stream (which I assume it does, since otherwise you wouldn't be able to pipe it to cat) then cat is entirely superfluous here - you can redirect standard output directly, specifying either absolute or relative path to your output file:


py EulerianCycle.py euleriancycle.txt > outputs/euleriancycleout.txt

(note: the directory outputs/ must already exist).




Neither of your other commands works the way you might imagine.



  • in py EulerianCycle euleriancycle.txt > cat euleriancycleout.txt, the shell creates a file named cat in the current directory, and redirects the output of py EulerianCycle to it, passing both euleriancycle.txt and euleriancycleout.txt to it as input arguments.



  • in py EulerianCycle.py euleriancycle.txt | cat >cd outputs/euleriancycleout.txt, the shell creates a file named cd in the current directory, cat reads outputs/euleriancycleout.txt and writes it to file cd, ignoring standard input from the pipe (cat only reads standard input when it is given no input files, or an explicit -).




Perhaps what you were aiming for here was to pipe the output to a subshell like:


py EulerianCycle.py euleriancycle.txt | (cd outputs; cat > euleriancycleout.txt)

or


py EulerianCycle.py euleriancycle.txt | (cd outputs && cat > euleriancycleout.txt)

Here, cat reads the subshell's standard input - which is provided by the pipe - after changing to the target directory. The second version only creates euleriancycleout.txt if the cd command succeeds; the first creates it in the current directory if the cd fails.


[#1377] Monday, December 20, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fulerio

Total Points: 172
Total Questions: 124
Total Answers: 109

Location: Hungary
Member since Thu, Aug 6, 2020
4 Years ago
fulerio questions
Thu, May 6, 21, 07:07, 3 Years ago
Wed, May 18, 22, 14:05, 2 Years ago
;