Working Ninja
2016-11-21T23:35:33

The Old Way

import os

os.system('find /home/user/documents -type f -iname "*.doc"')

The New, Recommended Way

import subprocess

subprocess.check_call([
    'find',
    '/home/user/documents',
    '-type', 'f',
    '-iname', '*.doc'
])

Bonus Tidbit

At this point, we can incorporate some nice exception handling:

import subprocess

try:
    subprocess.check_call([
        'find',
        '/home/user/documents',
        '-type', 'f',
        '-iname', '*.doc'
    ])
except subprocess.CallProcessError …

READ MORE