From b3cc717e39f12cabe6ddec1332399ce3ff52cea2 Mon Sep 17 00:00:00 2001 From: brunombpereira Date: Tue, 16 May 2023 18:42:18 +0100 Subject: [PATCH] [LABI] tema02: added cmd_comand.py to run command "ls -la" + sys.argv[1] --- 1ano/2semestre/labi/tema02/cmd_comand.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 1ano/2semestre/labi/tema02/cmd_comand.py diff --git a/1ano/2semestre/labi/tema02/cmd_comand.py b/1ano/2semestre/labi/tema02/cmd_comand.py new file mode 100644 index 0000000..8981dcd --- /dev/null +++ b/1ano/2semestre/labi/tema02/cmd_comand.py @@ -0,0 +1,18 @@ +import subprocess +import sys + +if len(sys.argv) < 3: + print("Usage: python Ex3.py ") + sys.exit(1) + +directory_path = sys.argv[1] +ignore_term = sys.argv[2] + +try: + output = subprocess.check_output(["ls", "-la", directory_path], universal_newlines=True) + lines = output.split("\n") + filtered_lines = [line for line in lines if ignore_term not in line] + print("\n".join(filtered_lines)) +except subprocess.CalledProcessError: + print("Error: could not list directory.") + sys.exit(1)