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 32,676 times.
Learn more...
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.
Steps
-
1Open 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.
-
Click the "Downloads" tab.
- 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 -
3
-
4Click "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.
-
1Decide 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
-
3
-
4
-
5
-
6
Expert Q&A
Tips
-
If your code is not working, make sure that all of your files are located in the same folder. This includes your actual code.Thanks
-
When opening a file, make sure to add ".txt" to the end of the file name.Thanks
-
Don't forget to close the quotation marks when opening a file.Thanks