Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
58 changes: 58 additions & 0 deletions list_files/README.md
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions list_files/list_files.py
Original file line number Diff line number Diff line change
@@ -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)