|
94 | 94 | ],
|
95 | 95 | "tags": ["python", "file", "write", "utility"],
|
96 | 96 | "author": "technoph1le"
|
| 97 | + }, |
| 98 | + { |
| 99 | + "title": "Find Files", |
| 100 | + "description": "Finds all files of the specified type within a given directory.", |
| 101 | + "code": [ |
| 102 | + "import os", |
| 103 | + "", |
| 104 | + "def find_files(directory, file_type):", |
| 105 | + " file_type = file_type.lower() # Convert file_type to lowercase", |
| 106 | + " found_files = []", |
| 107 | + "", |
| 108 | + " for root, _, files in os.walk(directory):", |
| 109 | + " for file in files:", |
| 110 | + " file_ext = os.path.splitext(file)[1].lower()", |
| 111 | + " if file_ext == file_type:", |
| 112 | + " full_path = os.path.join(root, file)", |
| 113 | + " found_files.append(full_path)", |
| 114 | + "", |
| 115 | + " return found_files", |
| 116 | + "", |
| 117 | + "# Example Usage:", |
| 118 | + "pdf_files = find_files('/path/to/your/directory', '.pdf')", |
| 119 | + "print(pdf_files)" |
| 120 | + ], |
| 121 | + "tags": ["python", "os", "filesystem", "file_search"], |
| 122 | + "author": "Jackeastern" |
97 | 123 | }
|
98 | 124 | ]
|
99 | 125 | },
|
|
0 commit comments