DFOR510 Week10 Python Files Hash Err
DFOR510 Week10 Python Files Hash Err
▪ Handling files
2
3
1. Libraries
❑ Identify standard vs. third party
❑ Import shortcuts and best practices
❑ Using the ‘os’ library for portability
2. Hashing…
❑ With Python libraries: hashlib & Crypto.Hash
3. File Handling
❑ Learn how to open, read, write, and append to files
❑ Identifying where file pointers are for various file modes
4. Error Handling
❑ Identify ‘Errors’ vs ‘Exceptions’
❑ Gracefully handle exceptions in Python scripts 4
5
6
▪ Use ‘import’ to include a particular library in your module
7
▪ Allows user to port python script easily between various
operating systems using os dependent functions
▪ os library functions:
▪ os.name – name of the operating system dependent module
imported
▪ os.path – yields path where Python is executed from
▪ os.chdir(“C:\”) – go to C:\ directory
▪ os.getcwd( ) – what directory are you currently working in
▪ os.walk(directory) – traverse directory’s folder from top-down
(default), or bottom-up
10
▪ Used to encode/decode ASCII text to binary formats.
Download:
❑ osBase64 (.ipynb or .py)
Download:
❑ hashWpython (.ipynb or .py)
19
20
http://www.99points.info/wp-content/uploads/2010/03/filetypes.png
File Mode Description
r Read – opens the file for read-only mode (default)
w Write – creates the file for writing; overwrites file it if already
exists
a Append – writes data at the end of a file; creates file if it
doesn’t already exist
rb, wb, or ab Binary mode – read or write file in binary mode
r+, rb+, w+, wb+, a+, or ab+ Read/Write – read and write files in standard or binary mode
21
▪ File object are created with open() method
▪ Syntax:
▪ open(filename, fileMode)
▪ inFile = open(‘week09_test.txt’)
▪ inFile = open(‘week09_test.txt’, ‘w’)
▪ inFile = open(‘week09_test.txt’, ‘rb’)
▪ inFile = open(‘week09_test.txt’, ‘a’)
22
http://www.potrebitel-russia.ru/uploads/images/t_7_t.jpg
Reading file objects Writing file objects
▪ read(), readline(), ▪ write(), writelines()
readlines() ▪ inFile.write(content)
▪ inFile.read() ▪ inFile.writelines(list_of_li
▪ Can specify number of nes)
bytes to be read
▪ inFile.readline()
▪ inFile.readlines()
23
https://thumbs.dreamstime.com/b/cartoon-open-book-bookmark-icon-isolated-white-background-eps-file-available-86598551.jpg
http://www.clker.com/cliparts/m/e/w/W/Z/3/pencil-and-paper-hi.png
▪ Python automatically closes files objects when done with
them
▪ However, good coding practice dictates we flush and close
file objects after writing to them:
▪ inFile.flush()
▪ inFile.close()
24
https://cdn-images-1.medium.com/max/670/1*wPqqYFfNreXF4INrNhYkeQ.jpeg
▪ When analyzing images or files, we may want to read files
in raw, or binary, format
▪ week09_1 = open(r‘C:\...\week09_image1.jpg’, ‘rb’)
▪ week09_1.tell()
▪ week09_1.read(# of bytes)
▪ week09_1.seek(offset, from_position)
▪ Where from_position is:
▪ 0 : from start of file - default
▪ 1: from current position
▪ 2: from end of file
25
http://inventionmachine.com/Portals/56687/images/engineers_seek.jpg
HANDLING FILES
From BB → Course Content → Class11… → Exercises
Download:
❑ handlingFiles (.ipynb or .py)
❑ lect11_1.jpg
❑ Lect11_test.txt (make a copy of this file after downloading)
27
28
Try/Except
▪ Errors – ▪ Exceptions –
29
ImportError/ import module doesn’t exist or couldn’t be
ModuleNotFound found
FileNotFoundError
ValueError trying to perform an illegal operation on an
object (e.g. int(“error”))
IndexError attempting to index an item in a location that
doesn’t exist
AttributeError trying to perform an illegal method on an object
TypeError e.g. list(1024) – trying to cast an integer as a list
NameError value doesn’t exist
31
▪ Unhandled
exception
▪ Handled
exception
32
▪ Why did this
code crash?
▪ What caused
the ‘except’
condition?
33
▪ Handling
multiple
exceptions
34
ERROR HANDLING
From BB → Course Content → Class10… → Exercise
Download:
❑ ErrorHandling (.ipynb or .py)
36
38
https://clipartfox.com/categories/view/8afc348758b19996c75d34ea32d903b144a67dca/spring-break-2017.html
Hashing
https://docs.python.org/3.7
https://pycryptodome.readthedocs.io/en/latest/src/hash/hash.html
https://pycryptodome.readthedocs.io/en/latest/src/hash/hash.html
Base64
https://blogs.oracle.com/rammenon/base64-explained
http://www.rfc-editor.org/rfc/rfc4648.txt
39
40
41