Text files can be super useful when coding in Python. You can retrieve information from text files using your code, and you can modify the information that is stored in a text file. In order to be able to do this, it is important to understand how to properly open the file, read the file or write in the file, and then finally save the changes you made (if you made any) to the file. This instruction set will teach you how to do this in a very simple way, and should take no longer than 10-15 minutes! These instructions will specifically discuss how to do this on a MacBook.

Part 1
Part 1 of 3:

Download Python

  1. 1
    Open a browser and navigate to Python.org.
    • This should take you to Python's website where you can download the latest version of Python available.
  2. 2
    Advertisement
  3. 3
  4. 4
  5. Advertisement
Part 2
Part 2 of 3:

Write Your Code

  1. 1
    Decide what you would like to create in a Text File.
    • For this example, The example below will demonstrate taking each line from a previously created file and printing every other line (every odd line) onto a new file.
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. Advertisement
Part 3
Part 3 of 3:

Run Your Program

  1. 1
    Make sure that every file you used is in the same folder.
    • This includes the file that your code is written on.
  2. 2
  3. Advertisement

About This Article

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, volunteer authors worked to edit and improve it over time. This article has been viewed 14,957 times.
How helpful is this?
Co-authors: 4
Updated: February 14, 2023
Views: 14,957
Categories: Python | Mac
Advertisement