Your own grep - using subprocess
Step 1
Use the script from the previous exercise and modify it.
Assign the return value of the subprocess.run()
to an variable and print it after clearing the terminal.
What do you get? Does it return the expected value?
Step 2
Determine how you could fetch the output which is now printed to stdout
and store it in a variable.1
The original ls
(or dir
) command has one new item every newline so you can store them easily in an list.2
Grep shows you every element which contains the passed string. Since we don’t know (yet) how to work with regular expressions, we will implement a more rudimentary version of grep which only displays exact name matches. Go through your list of items and print every item that contains the exact search string. 3
Step 3
To be more flexible, your script should take a path for the directory and a string to search for as input. The script should then run the command in the given directory4 and search for your string.
Step 4
Make sure that you won’t get trolled by any user and make sure, that the given directory is existing.5
If not, you should print an error message and quit the script.
-
You should use
subprocess.getoutput()
instead. ↩ -
Use
split()
on the saved output and split it at every newline character (\n
). ↩ -
Best way is to iterate over the items and see if your search string is
in
your item… ;) ↩ -
Do you remember the
cwd
argument? ↩ -
You can use
os.path.isdir()
to check that. Also see the docs for how this command works. ↩