Cool Computer Science Thing of the Day
No Reading Questions
No Quiz
File Input
To read from a file use built-in function to open it
file = open('data.txt', 'r')
- The first parameter is the file name
- The second is that we are going to read from the file (the os cares because depending on what you are doing it can be more or less efficient)
The file object is iterable
for line in file: print(line)
- Note the extra blank lines that are printed
Try printing just the last character of each line
for line in file: print('"' + line[-1] + '"')
- It is a new line character
This is where strip is useful
for line in file: print(line.strip())
- Removes leading and trailing whitespace (includes tabs a new lines)
Also should close it when you are done
for line in file: print(line.strip()) file.close()
This releases system resources, if you forget your program will work but will waste memory
File methods
Can also get the files contents as a string using read
text = file.read()
Or can get it as a list of strings by line
list_of_text = file.readlines()
Or read one line at a time
line_of_text = file.readline() another_line_of_text = file.readline()
readline returns empty string when done reading the file, so can use it in a while loop
line = file.readline() while len(line) != 0: line = file.readline() print(line.strip())
Note, all of these methods leave the new line character
File Output
- Writing is just as simple as reading
Just like read, need to open the file
file = open(‘data’, ‘w’)
- The ‘w’ parameter means write mode
- Note, if the file does not exist, it is created
- If it does exist, it overwrites it with a new file
To not overwrite, but to add to the end, open it in append mode
file = open(‘data’, ‘a’)
To actually write, use the write method
file.write('text to write')
- Note that it doesn’t write new line characters
Use the special text ‘’ to write new lines
file.write('text to write\n') file.write(text + '\n')
- Also note, closing the file is not optional
- Text to write to a file is stored in a buffer to make if more efficient
So if not closed and program ends, the text may not be written to the file
file = open(‘data’, ‘w’) for i in range(len(list_of_text)): file.write(list_of_text[i] + '\n') file.close()
Colors & PPM
- LCD screens are made up of lots of red, green, and blue lights
- Different combinations of red, green, and blue are interpreted by our brains as different colors
- Use numbers to encode brightness, often 0-255 or 0.0-1.0
- Example with an online color picker
- To display an image on the screen, need to set the brightness of all of the lights that make up the screen, so many, many numbers
- The PPM file format was originally created to solve operating system compatibility issuse when sending images over email.
- The PPM file solved this by sending images as text
- Encoding images as text is really inefficient, but it makes them easy for use to work with
PPM images start with the following lines:
P3 2 2 255
- The ‘P3’ is called the magic number, its a number when it is converted to binary
- It tells programs reading the file that it is a PPM file
- The ‘2 2’ is the dimensions of the image, width then height
- 255 is the range of red, green, and blue, values, 0 - 255, the 0 is implied
After that, just specify red, green, blue, for each pixel in row order
P3 2 2 255 255 0 0 0 255 0 0 0 255 0 0 0
- Can view the image in Ububut by double clicking the image
- Need to zoom in because it is so small
- Gimp can read and write PPM imagesf
- Find an image on google, open in Gimp, export to PPM
Emacs can even view the image by opening it, C-c C-c to toggle text and image mode