Best Python code snippet using lisa_python
tests_2.py
Source:tests_2.py
...3from problem_2 import find_files4class Test_FindFiles(unittest.TestCase):5 def test_should_return_empty_list_when_path_given(self):6 """7 Test find_files() should return an empty list when no path is given.8 """9 self.assertListEqual([], find_files('', ''))10 self.assertListEqual([], find_files('', None))11 self.assertListEqual([], find_files('', False))12 self.assertListEqual([], find_files('', []))13 self.assertListEqual([], find_files('.c', ''))14 self.assertListEqual([], find_files('.h', ''))15 self.assertListEqual([], find_files('.py', ''))16 self.assertListEqual([], find_files('.md', ''))17 def test_should_return_all_files_when_no_suffix_given(self):18 """19 Test find_files() should return a list of all files when no suffix is given.20 """21 expected = [22 './fixtures/problem_2/testdir/t1.c',23 './fixtures/problem_2/testdir/t1.h',24 './fixtures/problem_2/testdir/subdir2/.gitkeep',25 './fixtures/problem_2/testdir/subdir1/a.c',26 './fixtures/problem_2/testdir/subdir1/a.h',27 './fixtures/problem_2/testdir/subdir3/subsubdir1/b.c',28 './fixtures/problem_2/testdir/subdir3/subsubdir1/b.h',29 './fixtures/problem_2/testdir/subdir4/.gitkeep',30 './fixtures/problem_2/testdir/subdir5/a.c',31 './fixtures/problem_2/testdir/subdir5/a.h',32 ]33 expected.sort()34 actual = find_files('', './fixtures/problem_2')35 actual.sort()36 self.assertListEqual(expected, actual)37 actual = find_files('', './fixtures/problem_2/testdir')38 actual.sort()39 self.assertListEqual(expected, actual)40 def test_set_should_return_all_files_ending_in_c(self):41 """42 Test find_files() should return a list of all files that end with the .c extension.43 """44 expected = [45 './fixtures/problem_2/testdir/t1.c',46 './fixtures/problem_2/testdir/subdir1/a.c',47 './fixtures/problem_2/testdir/subdir3/subsubdir1/b.c',48 './fixtures/problem_2/testdir/subdir5/a.c',49 ]50 expected.sort()51 actual = find_files('c', './fixtures/problem_2')52 actual.sort()53 self.assertListEqual(expected, actual)54 actual = find_files('.c', './fixtures/problem_2')55 actual.sort()56 self.assertListEqual(expected, actual)57 actual = find_files('.c', './fixtures/problem_2/testdir')58 actual.sort()59 self.assertListEqual(expected, actual)60 def test_set_should_return_all_files_ending_in_h(self):61 """62 Test find_files() should return a list of all files that end with the .h extension.63 """64 expected = [65 './fixtures/problem_2/testdir/t1.h',66 './fixtures/problem_2/testdir/subdir1/a.h',67 './fixtures/problem_2/testdir/subdir3/subsubdir1/b.h',68 './fixtures/problem_2/testdir/subdir5/a.h',69 ]70 expected.sort()71 actual = find_files('h', './fixtures/problem_2')72 actual.sort()73 self.assertListEqual(expected, actual)74 actual = find_files('.h', './fixtures/problem_2')75 actual.sort()76 self.assertListEqual(expected, actual)77 actual = find_files('.h', './fixtures/problem_2/testdir')78 actual.sort()79 self.assertListEqual(expected, actual)80 def test_should_return_all_files_ending_in_gitkeep(self):81 """82 Test find_files() should return a list of all files that end with the .h extension.83 """84 expected = [85 './fixtures/problem_2/testdir/subdir2/.gitkeep',86 './fixtures/problem_2/testdir/subdir4/.gitkeep',87 ]88 actual = find_files('keep', './fixtures/problem_2')89 actual.sort()90 self.assertListEqual(expected, actual)91 actual = find_files('.gitkeep', './fixtures/problem_2/testdir')92 actual.sort()93 self.assertListEqual(expected, actual)94 def test_should_return_all_files_with_given_suffix(self):95 """96 Test find_files() should return a list of all files that end with the given suffix.97 """98 expected = [99 './problem_2.py',100 './tests_2.py',101 ]102 actual = find_files('_2.py', '.')103 self.assertListEqual(expected, actual)104 expected = [105 './explanation_2.md',106 './given_2.md',107 ]108 actual = find_files('_2.md', '.')109 self.assertListEqual(expected, actual)110if __name__ == '__main__':...
problem2.py
Source:problem2.py
1#2# File Recursion3#4import os 5def find_files(suffix, path):6 """7 Find all files beneath path with file name suffix.8 Note that a path may contain further subdirectories9 and those subdirectories may also contain further subdirectories.10 There are no limit to the depth of the subdirectories can be.11 Args:12 suffix(str): suffix if the file name to be found13 path(str): path of the file system14 Returns:15 a list of paths16 """17 paths = []18 if not os.path.isdir(path):19 if path.endswith(suffix):20 return [path]21 return []22 sub_paths = [ os.path.join(path, _sub_path) for _sub_path in os.listdir(path) ]23 for item in sub_paths:24 if os.path.isfile(item):25 if item.endswith(suffix):26 paths.append(item)27 else:28 paths += find_files(suffix, item)29 return paths30if __name__ == '__main__':31 path = 'testdir'32 suffix = '.c'33 # paths = find_files(suffix, path)34 # print(paths)35 # treee testdir36 # âââ subdir137 # â  âââ a.c38 # â  âââ a.h39 # âââ subdir240 # âââ subdir341 # â  âââ subsubdir142 # â  âââ b.c43 # â  âââ b.h44 # âââ subdir445 # âââ subdir546 # â  âââ a.c47 # â  âââ a.h48 # âââ t1.c49 # âââ t1.h50 print(f"\nTest0: find_files('.c','testdir')")51 # ['testdir/subdir3/subsubdir1/b.c', 'testdir/t1.c', 'testdir/subdir5/a.c', 'testdir/subdir1/a.c']52 print(find_files('.c','testdir'))53 print(f"\nTest1: find_files('.c','testdir/subdir5/subsub51')")54 # ['testdir/subdir5/subsub51/a51.c']55 print(find_files('.c','testdir/subdir5/subsub51'))56 print(f"\nTest2: find_files('.c','testdir/subdir4')")57 # []58 print(find_files('.c','testdir/subdir4'))59 print(f"\nTest3: find_files('.c','testdir/subdir432')")60 # []61 print(find_files('.c','testdir/subdir432'))62 print(f"\nTest4: find_files('.c','testdir/subdir5/nodir')")63 # []64 print(find_files('.c','testdir/subdir5/nodir'))65 print(f"\nTest5: find_files('.c','testdir/subdir5')")66 # ['testdir/subdir5/a.c','testdir/subdir5/subsub51/a51.c']...
find_files.py
Source:find_files.py
1import os2# Code to demonstrate the use of some of the OS modules in python3# Let us print all the files in the directory in which you are running this script4path = os.getcwd() + "\\testdir"5def find_files(suffix, path):6 # Edge cases7 if path is None:8 return "No path specified"9 elif type(path) != str:10 return "Path isn't a valid path"11 directories = os.listdir(path)12 files = []13 for dir in directories:14 dir_path = os.path.join(path, dir)15 if os.path.isdir(dir_path):16 files += find_files(suffix, dir_path)17 # Here we don't use append to avoid add empty string into our list18 elif os.path.isfile(dir_path) and dir_path.endswith(suffix):19 files.append(dir_path)20 return files21 """22 Find all files beneath path with file name suffix.23 Note that a path may contain further subdirectories24 and those subdirectories may also contain further subdirectories.25 There are no limit to the depth of the subdirectories can be.26 Args:27 suffix(str): suffix if the file name to be found28 path(str): path of the file system29 Returns:30 a list of paths31 """32# Edge test cases33print("find_files(, None):", find_files('', None), '\n')34print("find_files(, -1):", find_files('', -1), '\n')35# General test cases36print("find_files(\"\", .):", find_files("", "."), '\n')37print("find_files(\".py\", .):", find_files(".py", "."), '\n')38print("find_files(\".pdf\", .):", find_files(".pdf", "."), '\n')39print("find_files(\".c\", .):", find_files(".c", "."), '\n')...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!