<jupyter_start><jupyter_text>Getting Data  stdin and stdoutIf you run your Python scripts at the command line, you can pipe data through them using **sys.stdin** and **sys.stdout**.<jupyter_code>import sys, re
# sys.argv is the list of command-line arguments
# sys.argv[0] is the name of the program itself
# sys.argv[1] will be the regex specified at the command line
regex = sys.argv[1]
# for every line passed into the script
for line in sys.stdin:
    # if it matches the regex, write it to stdout
    if re.search(regex, line):
        sys.stdout.write(line)
# line_count.py
import sys

count = 0
for line in sys.stdin:
    count += 1

# print goes to sys.stdout
print(count)
type SomeFile.txt | python egrep.py "[0-9]" | python line_count.py<jupyter_output><empty_output><jupyter_text>This is a script that counts the words in its input and writes out the most common ones:<jupyter_code>import sys
from collections import Counter
# pass in number of words as first argument
try:
    num_words = int(sys.argv[1])
except:
    print("usage: most_common_words.py num_words")
    sys.exit(1)   # nonzero exit code indicates error

counter = Counter(word.lower()                      # lowercase words
                  for line in sys.stdin
                  for word in line.strip().split()  # split on spaces
                  if word)                          # skip empty 'words'

for word, count in counter.most_common(num_words):
    sys.stdout.write(str(count))
    sys.stdout.write("\t")
    sys.stdout.write(word)
    sys.stdout.write("\n")<jupyter_output><empty_output><jupyter_text>Reading Files The Basics of Text Files<jupyter_code># 'r' means read-only, it's assumed if you leave it out
file_for_reading = open('reading_file.txt', 'r')
file_for_reading2 = open('reading_file.txt')

# 'w' is write -- will destroy the file if it already exists!
file_for_writing = open('writing_file.txt', 'w')

# 'a' is append -- for adding to the end of the file
file_for_appending = open('appending_file.txt', 'a')

# don't forget to close your files when you're done
file_for_writing.close()<jupyter_output><empty_output><jupyter_text>Because it is easy to forget to close your files, you should always use them in a with block, at the end of which they will be closed automatically.  Delimited Files<jupyter_code>with open('tab_delimited_stock_prices.txt', 'w') as f:
    f.write("""6/20/2014\tAAPL\t90.91
6/20/2014\tMSFT\t41.68
6/20/2014\tFB\t64.5
6/19/2014\tAAPL\t91.86
6/19/2014\tMSFT\t41.51
6/19/2014\tFB\t64.34
""")

    def process(date: str, symbol: str, closing_price: float) -> None:
    # Imaginge that this function actually does something.
        assert closing_price > 0.0
import csv

with open('tab_delimited_stock_prices.txt') as f:
    tab_reader = csv.reader(f, delimiter='\t')
    for row in tab_reader:
        date = row[0]
        symbol = row[1]
        closing_price = float(row[2])
        process(date, symbol, closing_price)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup