---
title: How to read folder file contents into a text file with Python
description: How to read folder file contents into a text file with Python
---

[Skip to content](https://www.connysoderholm.com/blog/folder-file-contents-to-text-python#main-content)

![Conny-Söderholm](https://www.connysoderholm.com/hubfs/Conny-S%C3%B6derholm.svg)

- [Home](https://www.connysoderholm.com/)
- [Courses](https://www.connysoderholm.com/courses)
- [Articles](https://www.connysoderholm.com/blog)
- [Contact Me](https://www.connysoderholm.com/contact-me)

Open main navigation

Close main navigation

- [Home](https://www.connysoderholm.com/)
- [Courses](https://www.connysoderholm.com/courses)
- [Articles](https://www.connysoderholm.com/blog)
- [Contact Me](https://www.connysoderholm.com/contact-me)

 June 25, 2018

# How to read folder file contents into a text file with Python

![Picture of Conny Söderholm](https://app.hubspot.com/settings/avatar/c60eb763b898c1f4c2e54f9401f3aef1) [Conny Söderholm](https://www.connysoderholm.com/blog/author/conny-soderholm)

Share: [facebook-f icon](http://www.facebook.com/share.php?u=https://www.connysoderholm.com/blog/folder-file-contents-to-text-python) [linkedin-in icon](http://www.linkedin.com/shareArticle?mini=true&url=https://www.connysoderholm.com/blog/folder-file-contents-to-text-python) [Twitter icon](https://twitter.com/intent/tweet?url=https://www.connysoderholm.com/blog/folder-file-contents-to-text-python) [pinterest-p icon](http://pinterest.com/pin/create/link/?url=https://www.connysoderholm.com/blog/folder-file-contents-to-text-python) [envelope icon](mailto:?body=https://www.connysoderholm.com/blog/folder-file-contents-to-text-python)

*How to read folder file contents into a text file with Python *- Do you need to take all existing files in a folder and read them into a text file? You are one method away from it. In this post we are going to use the [listdir](https://docs.python.org/3/library/os.html) method from the os module to read all files in a folder. The listdir method returns a list containing the names of the entries in the directory given by path.

First, we import listdir from the os module. We then import the isfile and join from [os.path](https://docs.python.org/3/library/os.path.html). isfile checks that the file is really a file. the join method is used for especially joining os paths intelligently to avoid errors relating to file not found errors. The method getcwd returns the current working directory. Change this to your path if you do not want to run the script from the same folder all the time.

The last thing that we do is a list comprehension for the files. We read every file (f) for every file (f) in the current working directory returned by listdir (listdir(getcwd)). We only add f to the list if f is a regular file (if isfile(join(getcwd(), f))). We then create the file by reading each line in the onlyfiles list into the .txt file "output\_files.txt".

```
from os import listdir

from os.path import isfile, join

from os import getcwd

onlyfiles = [f for f in listdir(getcwd()) if isfile(join(getcwd(), f))]

with open("output_files.txt", "w") as f:
for line in onlyfiles:
f.write(line+"\n")
```

Happy coding!

Featured image from [Pixabay](https://pixabay.com/), [link](https://pixabay.com/photo-419043/)

[Python](https://www.connysoderholm.com/blog/tag/python)

## Related posts

[Python](https://www.connysoderholm.com/blog/tag/python)

## [Create folders from a text file, line by line with Python](https://www.connysoderholm.com/blog/create-folders-from-a-text-file-line-by-line-with-python)

![Picture of Conny Söderholm](https://app.hubspot.com/settings/avatar/c60eb763b898c1f4c2e54f9401f3aef1) [Conny Söderholm](https://www.connysoderholm.com/blog/author/conny-soderholm) 

 June 23, 2018

Create folders from a text file, line by line with Python - Do you sometimes need to create a lot...

[Read more](https://www.connysoderholm.com/blog/create-folders-from-a-text-file-line-by-line-with-python)

[Python](https://www.connysoderholm.com/blog/tag/python)

## [How to search for files on your computer faster with Python](https://www.connysoderholm.com/blog/search-files-on-your-computer-faster-with-python)

![Picture of Conny Söderholm](https://app.hubspot.com/settings/avatar/c60eb763b898c1f4c2e54f9401f3aef1) [Conny Söderholm](https://www.connysoderholm.com/blog/author/conny-soderholm) 

 June 28, 2018

How to search for files on your computer faster with Python When I use the Windows Explorer search...

[Read more](https://www.connysoderholm.com/blog/search-files-on-your-computer-faster-with-python)

[Python](https://www.connysoderholm.com/blog/tag/python), [pypdf2](https://www.connysoderholm.com/blog/tag/pypdf2)

## [Rotate PDF files with PyPdf2 and Tkinter!](https://www.connysoderholm.com/blog/rotate-pdf-files-with-pypdf2-and-tkinter)

![Picture of Conny Söderholm](https://app.hubspot.com/settings/avatar/c60eb763b898c1f4c2e54f9401f3aef1) [Conny Söderholm](https://www.connysoderholm.com/blog/author/conny-soderholm) 

 January 24, 2020

Rotating PDF files with PyPDF2 and Tkinter Introduction Sometimes we need simple tools to get the...

[Read more](https://www.connysoderholm.com/blog/rotate-pdf-files-with-pypdf2-and-tkinter)

[linkedin-in icon](https://www.linkedin.com/in/connysoderholm) [Follow us on Facebook](https://www.youtube.com/@connysoderholm1473)

- [Home](https://www.connysoderholm.com)
- [Courses](https://www.connysoderholm.com/courses)
- [Articles](https://www.connysoderholm.com/blog)
- [Contact Me](https://www.connysoderholm.com/contact-me)
- [Privacy Policy](https://www.connysoderholm.com/privacy-policy)
- [Disclaimer](https://www.connysoderholm.com/disclaimer)

---

[![Conny Söderholm Logo](https://www.connysoderholm.com/hubfs/Conny-S%C3%B6derholm.svg "Conny Söderholm Logo")](http://Conny%20Söderholm)

Copyright © 2026 Conny Söderholm

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Conny Söderholm",
    "url" : "https://www.connysoderholm.com/blog/author/conny-soderholm"
  },
  "dateModified" : "2023-07-31T15:02:37.471Z",
  "datePublished" : "2018-06-25T20:36:25.000Z",
  "headline" : "How to read folder file contents into a text file with Python",
  "mainEntityOfPage" : {
    "@id" : "https://www.connysoderholm.com/blog/folder-file-contents-to-text-python",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://www.connysoderholm.com/hubfs/Conny-S%C3%B6derholm.svg"
    },
    "name" : "Conny Söderholm"
  }
}
```