Input loop: Difference between revisions

Content added Content deleted
m (move fileinput to first part of python section since it's the most flexible)
(Undo revision 289780 by Maxr (talk) not necessarily any better at the started task though.)
Line 1,921: Line 1,921:


=={{header|Python}}==
=={{header|Python}}==

When you want to read from stdin, or (multiple) filenames are given on the command line:
<lang python>import fileinput
for line in fileinput.input():
pass # process line, includes newline</lang>
The fileinput module can also do inplace file editing, follow line counts, and the name of the current file being read etc.


To create a Python3 input loop use python's `input()` function.
To create a Python3 input loop use python's `input()` function.
Line 1,954: Line 1,948:
lines = my_file.readlines() # returns a list of the rest of the lines from the file</lang>
lines = my_file.readlines() # returns a list of the rest of the lines from the file</lang>
This does not mix well with the iteration, however.
This does not mix well with the iteration, however.


When you want to read from stdin, or (multiple) filenames are given on the command line:
<lang python>import fileinput
for line in fileinput.input():
pass # process line, includes newline</lang>
The fileinput module can also do inplace file editing, follow line counts, and the name of the current file being read etc.


=={{header|R}}==
=={{header|R}}==