From 81d318b728a6eeaf5e7d3e5c25443fec8cd5e9bd Mon Sep 17 00:00:00 2001 From: Darius Rapalis Date: Mon, 8 Sep 2025 16:11:52 +0300 Subject: [PATCH] list dirs --- .../get_files_info.cpython-312.pyc | Bin 0 -> 1939 bytes functions/get_files_info.py | 33 ++++++++++++++++++ tests.py | 30 ++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 functions/__pycache__/get_files_info.cpython-312.pyc create mode 100644 functions/get_files_info.py create mode 100644 tests.py diff --git a/functions/__pycache__/get_files_info.cpython-312.pyc b/functions/__pycache__/get_files_info.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a8099dec3d9ff36bf7d09b4158a79fbaa90a4de9 GIT binary patch literal 1939 zcmbVMO-~y~7@pY=uh$sX27@8N$>K&0jzSzzL>d~>rip`E5iY2eLR2B+U0@yKwRYBZ zY^|HBN-K~e5l%LW5Y8!7x$p}j+Jq=lFB>^g(3S&Id+E&zRI13OGsXs&pdxjoefOPr zp67W#_T$(3dI!Mk=IbYuKQI9NPBzs=Rh&%^c5*-h5{!WnAkh+a7mmO*3^0EZ$WWw; zB)d9%(fHcz#5Mh8Llb`_~0Et?lhY#zNOlIFQm>MOzti%wd36N#=N)G@;o%?8byT`l=zP<}U z)^XWjVkhLjA*JVjesoE_iJ|RkqCP#EtOK0pAC4a`_Cp;n-t~sZ= z#Ei2~99LrUC{i+Ve?}0;(z=XJWArH2>CgE3#IlGOjznaHh(3XfN_<=uJGWtA8#*#v zB~PHvqEuutiLgFtF;P{CTU>YymA4M0hcz9g6nw-g2#Z@BX|3!`$he`5Y}vwzgdCSF z8tIzFhi*sYgs!M@tn03HN2=QC&f;s_@nkGETF%pq6OO(Q7DyOZfHUpjI88>$n2sz~ zj_X?5a+Gg7XSSX0Ev8%)lo5I3#l5xtad1+dl7pHQ)|4a)j;X5NCCT4(g_W-G1QrU8 zC*u)PISP`5WMXKPjOzG-o^I$_JX>cEVlJee^H{i`XN|7@y+`R znPOYdecQUXEti^0ZoTQHR8=`9UtmtS3RwvZg%xFOCsGYiS*LMzrbH})6Xg9nv;HRgT!~T z*aqM>JQJWa8%|JuxIckS>;kPFn{E5WDOF0wcQw1>tXi-(rqIpC;nS EKMb9??f?J) literal 0 HcmV?d00001 diff --git a/functions/get_files_info.py b/functions/get_files_info.py new file mode 100644 index 0000000..21f9523 --- /dev/null +++ b/functions/get_files_info.py @@ -0,0 +1,33 @@ +import os + +def get_files_info(working_directory, directory="."): + try: + full_path = os.path.join(working_directory, directory) + + abs_working_directory = os.path.abspath(working_directory) + abs_target_path = os.path.abspath(full_path) + + if not abs_target_path.startswith(abs_working_directory): + return ( + f'Error: Cannot list "{directory}" as it is outside the permitted working directory' + ) + + if not os.path.isdir(abs_target_path): + return f'Error: "{directory}" is not a directory' + + results = [] + for entry in os.listdir(abs_target_path): + entry_path = os.path.join(abs_target_path, entry) + try: + is_dir = os.path.isdir(entry_path) + size = os.path.getsize(entry_path) + results.append( + f"- {entry}: file_size={size} bytes, is_dir={str(is_dir)}" + ) + except Exception as e: + results.append(f"- {entry}: Error accessing file info ({e})") + + return "\n".join(results) + + except Exception as e: + return f"Error: {e}" \ No newline at end of file diff --git a/tests.py b/tests.py new file mode 100644 index 0000000..c29a555 --- /dev/null +++ b/tests.py @@ -0,0 +1,30 @@ +from functions.get_files_info import get_files_info + + +def run_tests(): + cases = [ + ("calculator", "."), + ("calculator", "pkg"), + ("calculator", "/bin"), + ("calculator", "../"), + ] + + for working_dir, directory in cases: + print(f'get_files_info("{working_dir}", "{directory}"):') + + result = get_files_info(working_dir, directory) + + if directory == ".": + print("Result for current directory:") + else: + print(f"Result for '{directory}' directory:") + + # Indent each line of result + for line in result.splitlines(): + print(f" {line}") + + print() # Extra newline between test cases + + +if __name__ == "__main__": + run_tests() \ No newline at end of file