1

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 .
`
Peter Badida
  • 11,310
  • 10
  • 44
  • 90
Rainier
  • 11
  • 2
  • This isn't a Python issue; whatever your terminal emulator is, it's configured to close a window once the program running in that window completes. – chepner Jun 19 '20 at 11:05
  • Should I ask a different question on the website on how to configure this, or is there a quick answer? – Rainier Jun 19 '20 at 11:14
  • If you found a solution to your own question, please share it as an answer. Do not include the answer in the question. – Paolo Jun 19 '20 at 11:53
  • @Paolo : Is it okay like this? – Rainier Jun 19 '20 at 12:19

1 Answers1

0

Instead of running the script in Powershell which uses Command prompt to execute the script. Run the script directly in Command prompt so you can see the output.

To change terminal in Visual Studio Code: Click on the current terminal to see a dropdown menu and click "Select Default Shell". Choose "Command Prompt" from the options.

Rainier
  • 11
  • 2