Black Mamba

Faster, Higher, Stronger.

Python Notes II

Operation for text

  • Open and close a file
1
2
the_file = open('sketch.txt')
the_file.close()
  • Get and change the current working directory
1
2
3
4
import os
os.getcwd()
os.chdir('.../targetDirectory')
os.listdir()
  • Read line and back to the start location of the file
1
2
3
data = open('sketch.txt')
print(data.readline(), end='')
data.seek(0)    # “rewind” a file to the beginning
  • Process every line of a file
1
2
for eachLine in data:
    print(eachLine, end='')
  • Using split() method to process each line to extract part of the line
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import os

if os.path.exists('sketch.txt'):
    data = open('sketch.txt')

for eachLine in data:
    # only process the line that contains ':'
    if not eachLine.find(':') == -1:
        # optional arg is set to 1, line of data is only broken into 2 pieces
        (role, line_spoken) = eachLine.split(':', 1)
        line_spoken = line_spoken.strip()

        print(role, end='')
        print(' said: ', end='')
        print(line_spoken, end='')

    data.close()
else:
    print('The data file is missing!')

Handle Exceptions: try/except Machanism

  • Python try to run code first, then deal with runtime errors (exceptions) as they happen
1
2
3
4
try:
    code (which might cause a runtime error)
except ErrorType:
    error-recovery code
  • Python catch exceptions as they occur, gives you a chance to possibly recover from the error and, critically, not crash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
try:
    data = open('sketch.txt')

    for eachLine in data:
    try:
        (role, spoken) = eachLine.split(':', 1)
        line_spoken = line_spoken.strip()

        print(role, end='')
        print(' said: ', end='')
        print(spoken, end='')

    # for handling the case there is no ':' in a line
    except ValueError:
        pass

    data.close()
except IOError:
    print('The data file is missing!')

Extend try/except with finally

  • No matter what errors occur, code in the finally suite is always run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
man = []
other = []

try:
    data = open('sketch.txt')

    for eachLine in data:
    try:
        (role, spoken) = eachLine.split(':', 1)
        spoken = spoken.strip()

        if role == 'Man':
            man.append(spoken)
        elif role == 'Other Man':
            other.append(spoken)
        except ValueError:
            pass

    data.close()
except IOError:
    print('The data file is missing!')

try:
    man_file = open('man_data.txt', 'w')
    other_file = open('other_data.txt', 'w')
    print(man, file=man_file)
    print(other, file=other_file)
except IOError as err:
    print('File Error: ' + str(err))
finally:
    if 'man_file' in locals():
        man_file.close()
    if 'other_file' in locals():
        other_file.close()

Knowing the detailed error imformation

  • Python creates an exception object that is passed as an argument to the except suite
1
2
3
4
5
6
7
8
9
10
11
12
try:
    data = open('missingFile.txt')
        print(data.readline(), file=data)
# the error imformation is named as 'err'
except IOError as err:
    print('File error: ' + str(err))
finally:
    if 'data' in locals():
        data.close()

# output:
File error: [Errno 2] No such file or directory: 'missingFile.txt'

Use with to reduce the amount of code

  • The following code is identical to the former as a short version
1
2
3
4
5
try:
    with open('missingFile.txt', "w") as data:
        print(data.readline(), file=data)
except IOError as err:
    print('File error: ' + str(err))
  • try/except/finally code can be rewrite using with
1
2
3
4
5
6
try:
    with open('man_data.txt', 'w') as man_file, open('other_data.txt', 'w') as other_file:
        print(man, file=man_file)
        print(other, file=other_file)
except IOError as err:
    print('File error: ' + str(err))
  • Note: no need to close file, because with does that job

Open the file in write mode

  • Assume there is a file named “example.txt” in the current directory
1
2
3
out = open("example.txt", "w")
print("Write something to example.txt", file=out)
out.close()

Update nester.py for formating the output files

  • Add the 4th argument (fh=sys.out) be the output file
1
2
3
4
5
6
7
8
9
10
11
12
# nester.py
import sys

def print_item (the_list, indent=False, level=0, fh=sys.stdout):
    for each_item in the_list:
        if isinstance(each_item, list):
            print_item (each_item, indent, level+1, fh)
        else:
            if indent:
                for tab_stop in range(level):
                    print("\t", end='@!', file=fh)
            print (each_item, file=fh)
  • Using func print_item for formating the output file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import nester

man = []
other = []

try:
    data = open('sketch.txt')

for eachLine in data:
    try:
        (role, line_spoken) = eachLine.split(':', 1)
        line_spoken = line_spoken.strip()

        if role == 'Man':
            man.append(line_spoken)
        elif role == 'Other Man':
            other.append(line_spoken)
    except ValueError:
        pass
    data.close()
except IOError:
    print('The data file is missing!')

try:
    with open('man_data.txt', 'w') as man_file, open('other_data.txt', 'w') as other_file:
    # format the output files
    nester.print_item(man, fh=man_file)
    nester.print_item(other, fh=other_file)
except IOError as err:
    print('File error: ' + str(err))

Pickle the data

  • Store data using pickle.dump()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import pickle

man = []
other = []

try:
    ...
except IOError:
    ...

try:
    # 'wb' indicate the access mode to be "writeable, binary"
    with open('man_data.txt', 'wb') as man_file, open('other_data.txt', 'wb') as other_file:
    # store data
        pickle.dump(man, man_file)
        pickle.dump(other, other_file)
except IOError as err:
    print('File error: ' + str(err))
# handle pickle exceptions
except pickle.PickleError as perr:
    print('Pickling error: ' + str(perr))
  • Load data using pickle.load()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import pickle
import nester

new_man = []

try:
    with open('man_data.txt', 'rb') as man_file:
        new_man = pickle.load(man_file)
except IOError as err:
    print('File error: ' + str(err))
except pickle.PickleError as perr:
    print('Pickle error: ' + str(perr))

nester.print_item(new_man)

Bonus I: BIF split()

  • split(…)
1
str.split([sep[, maxsplit]]) -> list of strings
  • Return a list of the words in str, using sep as the delimiter string
  • If maxsplit is given, at most maxsplit splits are done
  • If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the results
  • If the optional argument is set to 1, line of data is only broken into 2 pieces, effectively negating the effect of any extra colon on any line

Bonus II: BIF strip()

  • strip(…)
1
2
3
str = "!!!!Today is a good day...Yeah.!!!!!!"
str.strip('!')
'Today is a good day...Yeah.'
  • Returns a copy in which all chars have been stripped at the beginning and the end

Bonus III: BIFs Recall

  • open()
  • close()
  • readline()
  • seek()
  • split()
  • strip()
  • find()
  • help()
  • not
  • pass
  • pickle.dump()
  • pickle.load()
  • sys.out
  • try/except/finally
  • with … as
  • ValueError: Occurs when data doesn’t conform to an expected format
  • IOError: Occurs when data can’t be accessed properly

Bonus IV: Python Variable

  • Python variables don’t actually contain the data assigned to them
  • Python variables contain a reference to a data object

Comments