Download Article
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.
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.
Advertisement
-
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
Advertisement
Expert Q&A
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
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
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.
Advertisement