# Find All File

## Find All File with .txt Extension Present Inside a Directory

* <https://www.programiz.com/python-programming/examples/find-txt-files>

In this example, you will learn to find all files with .txt extension present inside a directory.

To understand this example, you should have the knowledge of the following [Python programming](https://www.programiz.com/python-programming) topics:

* [Python File I/O](https://www.programiz.com/python-programming/file-operation)
* [Python Directory and Files Management](https://www.programiz.com/python-programming/directory)
* [Python for Loop](https://www.programiz.com/python-programming/for-loop)

### Example 1: Using glob

```python
import glob, os

os.chdir("my_dir")

for file in glob.glob("*.txt"):
    print(file)
```

**Output**

```
c.txt
b.txt
a.txt
```

### Example 2: Using os

```python
import os

for file in os.listdir("my_dir"):
    if file.endswith(".txt"):
        print(file)
```

**Output**

```
a
{21: 'b', 14: 'c'}
```

`pop()` also returns the value of the key passed.

### Using os.walk

```python
import os

for root, dirs, files in os.walk("my_dir"):
    for file in files:
        if file.endswith(".txt"):
            print(file)
```

**Output**

```
c.txt
b.txt
a.txt
```
