Download presentation
Presentation is loading. Please wait.
Published byShawn Stevenson Modified over 6 years ago
1
IST256 : Applications Programming for Information Systems
Files
2
Agenda Connection Activity Teaching: Practice Activity Strings
String Library Practice Activity Sentiment Analyzer
3
Connect Activity Go Over Quiz Average The Process: Inputs? Outputs?
Enter a quiz grade or type 'quit' : Enter a quiz grade or type 'quit' : Enter a quiz grade or type 'quit' : 40 Enter a quiz grade or type 'quit' : quit You took 3 quizzes Your average is 80 The Process: Inputs? Outputs? What is Needed? IO Loop? Write it without the loop then add the loop. YOU CANNOT WRITE IT IN ONE STEP… NEVER
4
Files == Persistence Files add a Persistence Layer to our programming where we can store our data after the program completes. We can also load other sets of data into our programs. When our program Stores data, we open the file for writing. When our program Reads data, we open the file for reading. To read or write a file we must first open it, which gives us a special variable called a file handle. We then use the file handle to read or write from the file. The read() function reads from the write() function writes to the file through the file handle.
5
Watch Me Code Let’s Write two programs.
One to save a password to a file One to check the password as read from the file # Input Password from user and write it to a file. password = input("Enter your new password:") with open ("password.txt","w") as file: file.write(password) print("Password Saved!") def readpassword(): with open("password.txt","r") as file: password = file.read() return password password = readpassword() print (password)
6
Your Operating System and You
Files are stored on your hard disk in folders. When the python program is in the same folder as the file, no path is required. When the file is in a different folder not, a path is required. Absolute paths point to a file starting at the root of the hard disk. Relative paths point to a file starting at the current place on the hard disk.
7
Python Path Examples What Windows Mac / Linux File in current folder
“file.txt” File up one folder from the current folder “../file.txt” File down two folders from the current folder “folder1/folder2/file.txt” Absolute path to file in a folder “C:/folder1/file.txt” “/folder1/file.txt”
8
Watch Me Code Let’s Write two programs.
One to save a password to a file One to check the password as read from the file Files and Exceptions # Input Password from user and write it to a file. password = input("Enter your new password:") with open ("password.txt","w") as file: file.write(password) print("Password Saved!") def readpassword(): with open("password.txt","r") as file: password = file.read() return password password = readpassword() print (password)
9
Help me Code Create a file with a list of quiz grades, one per line. Write a program to read the file and output the average of all the quizzes.
10
Prep: Now You Code Getting the Data File:
Open a web browser to: Highlight all the text on the page (CTRL+A does this on a PC) Copy the text (CTRL+C does this on a PC) From Jupyter, create a new text file Paste in the text (CTRL+V does this on a PC) Save the file as mbox-short.txt
11
Now You Code Email Spammer Training 101
Write a program to “harvest” all of the s from the mbox-short.txt file. Specifically: Look for lines beginning with “From:” and extract the address from those lines. Suggested Approach Write the program to read the file and print EVERY line. Modify the program to only print the “From:” lines Modify the program to only print the s. Sell the list on the open market and PROFIT!!!!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.