views
Download Python
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.
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.Python Website.jpg
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. IDLE.png
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.Screen Shot 2022 02 19 at 6.48.23 PM.png
Write Your Code
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.
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')".Screen Shot 2022 03 06 at 6.18.55 PM.png The letter "r" in this code stands for read.
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')".Screen Shot 2022 03 06 at 6.19.14 PM.png The letter "w" in this code stands for write.
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()".Screen Shot 2022 03 06 at 6.19.33 PM.png This is a variable that can now be used to read each line separately in the text file you are reading.
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.Screen Shot 2022 03 06 at 7.16.39 PM.png
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.Screen Shot 2022 03 06 at 6.20.03 PM.png This is an important habit you should have, as it reduces the risk of the file being unwarrantedly modified or read.
Run Your Program
Make sure that every file you used is in the same folder. This includes the file that your code is written on.
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.Screen Shot 2022 03 06 at 6.41.23 PM.png
Comments
0 comment