Dark theme

Key Ideas


The key ideas for this part centre on file reading and writing


Redirecting standard i/o

Here's a couple of files to help with experimenting with this: stdio.py; stdin.txt. Try running them like this:

Standard i/o
python stdio.py

Stdin from file:
python stdio.py < stdin.txt

Stdout to overwritten file:
python stdio.py > stdout.txt

Stdout to appended file:
python stdio.py >> stdout.txt

Both:
python stdio.py < stdin.txt > stdout.txt

Piping between programs using "|" (SHIFT- backslash on most Windows keyboards):
python stdio.py | python stdio.py


Generic file reading and writing

The following code reads and writes space delimited ints:

with open("data.txt") as f:
    data = []
    for line in f:
        parsed_line = str.split(line," ")
        data_line = []
        for word in parsed_line:
            data_line.append(int(word))
        data.append(data_line)
    print(data)
with open("dataout.txt", "w") as f:
    for line in data:
        for value in line:
            f.write(str(value) + " ")
        f.write("\n")

The following code reads multiple files:

import fileinput
a = ["file1.txt", "file2.txt", "file3.txt", "file4.txt"]
b = fileinput.input(a)
for line in b:
    print(b.filename())
    print(line)
b.close()

The following code writes multiple files:

import fileinput
a = ["file1.txt", "file2.txt", "file3.txt", "file4.txt"]
b = fileinput.input(a, inplace=1, backup='.bak')
for line in b:
    print("new text")
b.close()


CSV file reading and writing

This is probably the most useful reading and writing code.

import csv
with open('data.csv', newline='') as f:
    reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
    for row in reader:
        for value in row:
            print(value)

The kwarg quoting=csv.QUOTE_NONNUMERIC converts numbers into floats. Remove to keep the data as strings. Note that there are different dialects of csv which can be accounted for (info). For example, add dialect='excel-tab' to the reader to open tab-delimited files.

with open('dataout.csv', 'w', newline='') as f2:     writer = csv.writer(f2, delimiter=' ')     for row in data:         writer.writerow(row) # List of values.

The optional delimiter here creates a space delimited file rather than csv.


File system navigation

Ways of making a path:

import pathlib
p = pathlib.Path('c:/Program Files')
p = pathlib.Path.cwd() # Current working directory
pathlib.Path.home() # User home

a = os.path.join(pathlib.Path.cwd().anchor, 'Program Files', 'Notepad++') # drive + root, 'Program Files', and 'Notepad++'
p = pathlib.Path(a)

Traversing directory trees: getting subdirectories:

import pathlib
p = pathlib.Path('.') # Current directory
for x in p.iterdir():
    if x.is_dir(): # see also x.is_file()
    print(x)

Traversing directory trees: useful code for traversing a directory tree (here to remove files):

import os
for root, dirs, files in os.walk(deletePath, topdown=False):
    for name in dirs:
        os.rmdir(os.path.join(root, name))
    for name in files:
        os.remove(os.path.join(root, name))

The glob library can be used to traverse a single or recursive directory structure to find files and directories matching a pattern. For example, to build a list all the files in a pathlib.Path that have a specific file extension:

a = list(p.glob('**/*.txt'))

The "**" pattern makes it recursively check the path directory and all subdirectories.

a = sorted(Path('.').glob('*.csv'))

Gives a sorted list of csv files in the current directory.