Download presentation
Presentation is loading. Please wait.
1
Python 14 Mr. Husch
2
Aim This exercise will introduce students to reading files.
Understand the commands to open a file in Python
3
Activity What uses do the argv and raw_input ( ) commands have. You are going to be utilizing these commands to ultimately read files. Also, be cautious because working with files is an easy way to erase your work completely.
4
Lastname14.py
5
Lastname114.py Don’t forget the extra 1
6
Things to look at Line 1-3 should be a familiar use of argv to get a filename. Next we have line 5 where we use a new command open. Right now, run pydoc open and read the instructions. Notice how like your own scripts and raw_input, it takes a parameter and returns a value you can set to your own variable. You just opened a file. Line 7 we print a little line, but on line 8 we have something very new and exciting. We call a function on txt. What you got back from open is a file, and it's also got commands you can give it. You give a file a command by using the . (dot or period), the name of the command, and parameters. Just like with open and raw_input. The difference is that when you say txt.read() you are saying, "Hey txt! Do your read command with no parameters!"
7
Run in CMD To run this in CMD you need to go to your folder directory that you saved the game in. You need to type in python lastname114.py lastname14.py This will open both files, with 14 opening up inside 114.
8
When you run it you should see something like this
Here's your file 'lesson14_sample.txt': This is stuff I typed and it’s very cool. Type the filename again: > ex15_sample.txt
9
Try and open an old program inside 114
Instead of using python 114.py 14.py use python 114.py and then an older file to see if it will open.
10
Comments in python, you need to use a # to make a comment.
#This would be a comment in Python
11
Before turning it 1) Write what each line of code does in a comment line. 2) Try to script the raw_input ( ) section on lines 10 – 15
12
After turning in Keep your program up and running, we are going to have a “science fair” style showing of what you made. You will show the class your program, how you run in in CMD and the program that you choose to open in 114.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.