diff --git a/.gitignore b/.gitignore index d9005f2..3eb56cb 100644 --- a/.gitignore +++ b/.gitignore @@ -149,4 +149,4 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +.idea/ diff --git a/list_files/README.md b/list_files/README.md new file mode 100644 index 0000000..dd6ba6c --- /dev/null +++ b/list_files/README.md @@ -0,0 +1,58 @@ +# List Files Script + +This is a simple Python script that lists all files and directories in a specified folder. + +## Features + +* Lists files and folders in any directory +* Handles errors (missing folder or no access) +* Interactive input (asks user for a path) +* Beginner-friendly and easy to understand + +## Usage + +Run the script: + +```bash +python list_files.py +``` + +You will be prompted to enter a folder path: + +``` +Enter folder path (leave empty for current directory): +``` + +* Press **Enter** to use the current directory +* Or type a path, for example: + +``` +/home/user +``` + +## Example Output + +``` +file1.py +file2.txt +folder1 +``` + +## Using in Code + +You can also import and use the function in your own Python code: + +```python +from list_files import list_files + +list_files("your/folder/path") +``` + +## Requirements + +* Python 3.x + +## Notes + +* If the directory does not exist, an error message will be shown +* If access is denied, the script will notify you diff --git a/list_files/list_files.py b/list_files/list_files.py new file mode 100644 index 0000000..654a5ca --- /dev/null +++ b/list_files/list_files.py @@ -0,0 +1,22 @@ +import os + + +def list_files(path="."): + """Print all files and directories in the given folder""" + try: + files = os.listdir(path) + for file in files: + print(file) + except FileNotFoundError: + print(f"Directory '{path}' not found.") + except PermissionError: + print(f"No permission to access '{path}'.") + + +if __name__ == "__main__": + user_input = input("Enter folder path (leave empty for current directory): ").strip() + + if user_input == "": + list_files() + else: + list_files(user_input)