developing in 60 minutes: linux and python slides
TRANSCRIPT
![Page 1: Developing in 60 Minutes: Linux and Python slides](https://reader034.vdocument.in/reader034/viewer/2022042618/58a92fce1a28ab3f778b50c5/html5/thumbnails/1.jpg)
Developing in 60 MinutesLinux and PythonLinux and Python
Developing in 60 Minutes
Chapter Six: Tutorial
![Page 2: Developing in 60 Minutes: Linux and Python slides](https://reader034.vdocument.in/reader034/viewer/2022042618/58a92fce1a28ab3f778b50c5/html5/thumbnails/2.jpg)
The material in this SlideShare is developed from the book:
“Developing in 60 Minutes: Linux and Python” by Jedidiah Reeser
JedidiahReeser.com/books
Also available on Amazon.com
![Page 3: Developing in 60 Minutes: Linux and Python slides](https://reader034.vdocument.in/reader034/viewer/2022042618/58a92fce1a28ab3f778b50c5/html5/thumbnails/3.jpg)
Additional Media
Sample files and a demo video are available at:
JedidiahReeser.com/books
![Page 4: Developing in 60 Minutes: Linux and Python slides](https://reader034.vdocument.in/reader034/viewer/2022042618/58a92fce1a28ab3f778b50c5/html5/thumbnails/4.jpg)
To begin, open your text editor and type in the sample file below and save as “dev_in_60_sample.txt”.
![Page 5: Developing in 60 Minutes: Linux and Python slides](https://reader034.vdocument.in/reader034/viewer/2022042618/58a92fce1a28ab3f778b50c5/html5/thumbnails/5.jpg)
Now open up a new file and save it as“dev_in_60.py”
In your editor, add the following line of code, as the first line of the “dev_in_60.py”. The first line is the shebang line and will indicate that the script should use the Python interpreter for processing the commands in the file.
#!/usr/bin/env python
![Page 6: Developing in 60 Minutes: Linux and Python slides](https://reader034.vdocument.in/reader034/viewer/2022042618/58a92fce1a28ab3f778b50c5/html5/thumbnails/6.jpg)
Now add the code below to open the file so you can access its contents. In order to open a file, you can use the “open” method. We pass in the name of the file, "dev_in_60_sample.txt”, and that we want to append, “a”, to the file. If you use the “with” command, it will help ensure that your file safely closes and frees up system resources.
with open(‘dev_in_60_sample.txt’, 'a') as f:
![Page 7: Developing in 60 Minutes: Linux and Python slides](https://reader034.vdocument.in/reader034/viewer/2022042618/58a92fce1a28ab3f778b50c5/html5/thumbnails/7.jpg)
Next, add the text “Hello World!” to the end of the “dev_in_60_sample.txt” file by indenting with four spaces and typing the following line:
Notice how the string ends with “\n”? That will add a newline character.
f.write('Hello World!\n')
![Page 8: Developing in 60 Minutes: Linux and Python slides](https://reader034.vdocument.in/reader034/viewer/2022042618/58a92fce1a28ab3f778b50c5/html5/thumbnails/8.jpg)
The script now opens the file and appends a line of text to the end. To confirm that the code was successfully implemented, you will need to open the file and print out the lines onto the screen.
To open the file with read only access, use the with open command used earlier and change the “a” to a “r”.
with open(‘dev_in_60_sample.txt’, 'r') as f:
![Page 9: Developing in 60 Minutes: Linux and Python slides](https://reader034.vdocument.in/reader034/viewer/2022042618/58a92fce1a28ab3f778b50c5/html5/thumbnails/9.jpg)
To iterate over each line in the file and print it on the terminal, you can treat the file like a list. In Python, you can use a for loop to iterate over the file. It is as simple as:
for line in f: print line
![Page 10: Developing in 60 Minutes: Linux and Python slides](https://reader034.vdocument.in/reader034/viewer/2022042618/58a92fce1a28ab3f778b50c5/html5/thumbnails/10.jpg)
![Page 11: Developing in 60 Minutes: Linux and Python slides](https://reader034.vdocument.in/reader034/viewer/2022042618/58a92fce1a28ab3f778b50c5/html5/thumbnails/11.jpg)
To test the script, run the following command in the directory where you made the file:
python dev_in_60.py
Your screen output should look like the image below:
![Page 12: Developing in 60 Minutes: Linux and Python slides](https://reader034.vdocument.in/reader034/viewer/2022042618/58a92fce1a28ab3f778b50c5/html5/thumbnails/12.jpg)
Congratulations! You have finished writing your first Python script in
the Linux environment.
![Page 13: Developing in 60 Minutes: Linux and Python slides](https://reader034.vdocument.in/reader034/viewer/2022042618/58a92fce1a28ab3f778b50c5/html5/thumbnails/13.jpg)
jedidiahreeser.com/books