I'm still relatively new to Python. I'm supposed to write a Python script that prints out all files and directories under certain conditions: You can find the explanation of the exercise below.
The solution to the exercise of my course Scripting-languages doesn't seem to print the actual results and I've searched and tried but I don't see what's wrong with it. After all it's supposed to be the solution.
The supposed solution:
from pathlib import Path
import argparse
from pathlib import Path
import os
import re
def minimum_size(size):
def check(filename):
return os.path.getsize(filename) >= size
parser = argparse.ArgumentParser(prog='find')
parser.add_argument('path', default='.')
parser.add_argument('--minimum-size', dest='minimum_size', default=0, type=int)
parser.add_argument('--maximum-size', dest='maximum_size', default=float('inf'), type=int)
parser.add_argument('--no-directories', dest='no_directories', default=False, action='store_true')
parser.add_argument('--no-files', dest='no_files', default=False, action='store_true')
parser.add_argument('--extension', dest='extension')
parser.add_argument('--contains', dest='contains')
args = parser.parse_args()
path = Path(args.path)
for entry in path.glob('**/*'):
if not os.path.getsize(entry) >= args.minimum_size:
continue
if not os.path.getsize(entry) <= args.maximum_size:
continue
if args.no_directories and os.path.isdir(entry):
continue
if args.no_files and os.path.isfile(entry):
continue
if args.extension and not entry.suffix == args.extension:
continue
if args.contains:
if not os.path.isfile(entry):
continue
with open(entry, 'r') as file:
contents = file.read()
if not re.search(args.contains, contents):
continue
print(entry)
The explanation:
My own comments about this: I think the examples are supposed to include the extension when calling the file and so I do do that when trying to execute the code. I even include the whole path to the file so that I won't get an 'ObjectNotFound' error. When I run the solution in Powershell in Python, the command prompt opens and closes immediately. When I run it directly in Powershell, the command prompt opens, lists all the output and closes immediately when it's done. If you could give me a way to just stop the command prompt from closing so that I can see the result. I'd already be very happy.
Write a script `find.py` that prints out all files and directories
that satisfy certain conditions.
The following functionality must be supported:
* One positional parameter that specifies the directory in which to start looking recursively.
* `--minimum-size=N`: only files whose size is at least `N` must be listed.
* `--maximum-size=N`: only files whose size is at most `N` must be listed.
* `--no-directories`: don't list directories.
* `--no-files`: don't list files.
* `--extension=EXT`: only list entries with extension `EXT`
* `--contains=REGEX`: only list files whose contents match `REGEX`.
To get a better feel for how your script should work, feel free
to experiment with the solution. For example, try
`bash
# List everything starting from two directories up
$ python find ../..
# List all python files in current directory
$ python find --extension=.py .
`