PDF download Download Article PDF download Download Article

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

PDF download Download Article
  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.
    • Find where it says "Download the latest version for macOS".
    • Click the yellow button to download the latest version of Python onto your MacBook.
    Advertisement
  2. 3
    Go to your MacBook's "Launchpad" and open up "IDLE".
    • This will open up an IDLE Shell where it will output the code that you run.
  3. 4
    Click "File" and then "New File" in the upper left-hand corner of your screen
    • This will open a blank terminal where you can start typing your Python code.
  4. Advertisement
Part 2
Part 2 of 3:

Write Your Code

PDF download Download Article
  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
    Open the text file you would like to use.
    • Use any variable you like (example: "oldfile"). Assign this to "open("YOUR FILE NAME.txt", 'r')".
    • The letter "r" in this code stands for read.
  3. 3
    Assign a new variable for the new file that you are creating
    • Use any variable you like different from the first one (example: "newfile") and assign it to "open("NEW FILE NAME YOU ARE CREATING.txt", 'w')".
    • The letter "w" in this code stands for write.
  4. 4
    Assign a new variable to read the lines of the file
    • Use any new variable (example: "eachline") and assign it to your original file variable followed by ".readlines()".
    • This is a variable that can now be used to read each line separately in the text file you are reading.
  5. 5
    Write the code that you want to implement into the file
    • The example contains a for loop that took the location of each line, decided if it was an odd line, and then used the new text file variable ".write(NAME OF FOR LOOP) to write every other line onto the new text file.
  6. 6
    Close files you opened
    • At the end of your code, write your text file variable ".close()".
    • Do this for each file that you previously opened in your code.
    • This is an important habit you should have, as it reduces the risk of the file being unwarrantedly modified or read.
  7. Advertisement
Part 3
Part 3 of 3:

Run Your Program

PDF download Download Article
  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
    Click "Run" at the top of your screen, and then "Run Module".
    Screen Shot 2022 02 19 at 6.56.47 PM.png
    • This is the final step that runs your program,
    • Whatever files you wanted to write in should now be created after running this program.
  3. Advertisement

Expert Q&A

Ask a Question
      Advertisement

      Tips


      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!
      Advertisement

      About This Article

      Thanks to all authors for creating a page that has been read 31,672 times.

      Is this article up to date?

      Advertisement