[LABI] tema02: added cmd_comand.py to run command "ls -la" + sys.argv[1]

This commit is contained in:
brunombpereira 2023-05-16 18:42:18 +01:00
parent c49a773e0e
commit b3cc717e39
1 changed files with 18 additions and 0 deletions

View File

@ -0,0 +1,18 @@
import subprocess
import sys
if len(sys.argv) < 3:
print("Usage: python Ex3.py <directory_path> <ignore_term>")
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)