Python Course

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

Python course

tko preživi, pričat će ...

by Ivan Radić

1 / 43
le system: >>> import os
>>> os.listdir("C:\\")
directories ['$Recycle.Bin', '.rnd', '1', 'Boot', 'bootmgr', 'BOOT_SAV.BOT',
'compiler', 'Documents and Settings', 'hiberfil.sys',
'installTuner.log', 'Intel', 'local_data', 'MSOCache', 'ORACLE',
'pagefile.sys', 'PerfLogs', 'Program Files', 'Program Files (x86)',
'ProgramData', 'PROT_INS.SYS', 'Recovery', 'Ruby21',
'System Volume Information', 'Temp', 'Users', 'VOL_CHAR.DAT',
'Windows']

Lists are easy to sort in Python:

>>> root = "C:\\"


>>> sorted(os.listdir(root))

['$Recycle.Bin', '.rnd', '1', 'BOOT_SAV.BOT', 'Boot',


'Documents and Settings', 'Intel', 'MSOCache', 'ORACLE',
'PROT_INS.SYS', 'PerfLogs', 'Program Files', 'Program Files (x86)',
'ProgramData', 'Recovery', 'Ruby21', 'System Volume Information',
'Temp', 'Users', 'VOL_CHAR.DAT', 'Windows', 'bootmgr', 'compiler',
'hiberfil.sys', 'installTuner.log', 'local_data', 'pagefile.sys']

2 / 43
le system: Since os.listdir creates a list we can easily loop over the list, and generate full path using handy module
os.path and it's function join:
directories >>> for f in sorted(os.listdir(root)):
print os.path.join(root, f)

C:\$Recycle.Bin
C:\.rnd
C:\1
C:\BOOT_SAV.BOT
C:\Boot
C:\Documents and Settings
C:\Intel
C:\MSOCache
C:\ORACLE
C:\PROT_INS.SYS
C:\PerfLogs
C:\Program Files
C:\Program Files (x86)
C:\ProgramData
C:\Recovery
C:\Ruby21
C:\System Volume Information
C:\Temp
C:\Users
C:\VOL_CHAR.DAT
C:\Windows
C:\bootmgr
C:\compiler
C:\hiberfil.sys
C:\installTuner.log
C:\local_data
C:\pagefile.sys

3 / 43
le system: Module os.path has few more interesting functions:

directories This is how we can filter files only:

>>> for f in sorted(os.listdir(root)):


full = os.path.join(root, f)
if os.path.isfile(full):
print full
C:\.rnd
C:\BOOT_SAV.BOT
C:\PROT_INS.SYS
C:\VOL_CHAR.DAT
C:\bootmgr
C:\hiberfil.sys
C:\installTuner.log
C:\pagefile.sys

and directories:

>>> for f in sorted(os.listdir(root)):


full = os.path.join(root, f)
if os.path.isdir(full):
print full

C:\$Recycle.Bin
C:\1
C:\Boot
C:\Documents and Settings
C:\Intel
C:\MSOCache
C:\ORACLE
C:\PerfLogs
C:\Program Files
C:\Program Files (x86) 4 / 43
le system: Function os.listdir has few drawbacks (it can't filter results and it doesn't return full path).

directories Module glob solves both issues.


"glob" is short for "globbing" i.e. file search with UNIX wildcard characters.

>>> import glob


>>> glob.glob("C:\\*.sys")

['C:\\hiberfil.sys', 'C:\\pagefile.sys', 'C:\\PROT_INS.SYS']

Full list of wildcard characters:

*
matches zero or more characters
*.mp3
any_song_title.mp3
?
matches one character
song.mp?
song.mp3, song.mp4
[...]
matches any character listed in brackets
[AB]*
any file name starting with A or B
[!...]
matches any character not listed in brackets
*[!A]
any file name that doesn't end with A

5 / 43
le system: Class os.path is very useful:

paths >>> root = "C:\\my_folder\\my_file.txt"

>>> os.path.basename(root)
'my_file.txt'

>>> os.path.dirname(root)
'C:\\my_folder'

>>> os.path.split(root)
('C:\\my_folder', 'my_file.txt')

>>> folder, file_name = os.path.split(root)

>>> os.path.splitext(root)
('C:\\my_folder\\my_file', '.txt')

>>> os.path.isfile(root); os.path.isdir(root); os.path.exists(root)


False False False

>>> os.path.join("C:\\", "folder", "subfolder", "file.txt")


'C:\\folder\\subfolder\\file.txt'
>>>
>>> os.path.join("C:\\", "folder\\", "subfolder\\", "file.txt")
'C:\\folder\\subfolder\\file.txt'

P.S. Only first path element can have a leading slash. 6 / 43


le system: BTW, MS Windows does support forward slash as separator in paths:

paths >>> root = "C:\\my_folder\\my_file.txt"

is just as good as:

>>> root = "C:/my_folder/my_file.txt"

you can even mix forward and backward slashes:

>>> root = "C:\\my_folder/my_file.txt"

try to type paths with forward slashes in Windows explorer, verify that they work :-)

In this course we will use Windows separators (backward slashes), but if unix-like separator sneaks in, it is still
a valid path.

7 / 43
le system: dir Directory walker is a recursive function that can automatically list all files and subdirectories.

walker >>> start = "C:\\Program Files (x86)\\Python 2.7.9"


>>> for root, dirs, files in os.walk(start):
print root
print dirs
print files
print "-" * 15

C:\Program Files (x86)\Python 2.7.9


['DLLs', 'Doc', 'include', 'Lib', 'libs', ...]
['LICENSE.txt', 'NEWS.txt', 'python.exe', ...]
---------------
C:\Program Files (x86)\Python 2.7.9\Lib
['bsddb', 'compiler', 'ctypes', 'curses', ...]
['abc.py', 'abc.pyc', 'aifc.py', 'antigravity.py', ...]
---------------
C:\Program Files (x86)\Python 2.7.9\Lib\bsddb
['test']
['db.py', 'dbobj.py', 'dbrecio.py', 'dbshelve.py', ...]
---------------
C:\Program Files (x86)\Python 2.7.9\Lib\ctypes
['macholib', 'test']
['util.py', 'util.pyc', 'wintypes.py', 'wintypes.pyc', ...]
---------------
C:\Program Files (x86)\Python 2.7.9\Lib\distutils
['command', 'tests']
['archive_util.py', 'archive_util.pyc', 'bcppcompiler.py', ...]
---------------
... and so on, many, many more ...

8 / 43
le system: dir If you wish to skip some subdirectories, just remove them from dirs list:

walker >>> for root, dirs, files in os.walk(start):


print root; print dirs; print files; print "-" * 15
if "Lib" in dirs:
dirs.remove("Lib")
if "tcl" in dirs:
dirs.remove("tcl")
if "Tools" in dirs:
dirs.remove("Tools")

C:\Program Files (x86)\Python 2.7.9


['DLLs', 'Doc', 'include', 'Lib', 'libs', 'Scripts', 'tcl', 'Tools']
['LICENSE.txt', 'NEWS.txt', 'python.exe', ...]
---------------
C:\Program Files (x86)\Python 2.7.9\DLLs
[]
['bz2.pyd', 'py.ico', 'pyc.ico', 'pyexpat.pyd', ...]
---------------
C:\Program Files (x86)\Python 2.7.9\Doc
[]
['python279.chm']
---------------
C:\Program Files (x86)\Python 2.7.9\include
[]
['abstract.h', 'asdl.h', 'ast.h', 'bitset.h', ...]
---------------
C:\Program Files (x86)\Python 2.7.9\libs
[]
['bz2.lib', 'libpython27.a', 'pyexpat.lib', ...]
---------------
C:\Program Files (x86)\Python 2.7.9\Scripts
[]
['easy_install-2.7.exe', 'easy_install.exe', ...]
---------------
9 / 43
le system: Python has two functions that create directories:

mkdir >>> import os


>>> os.mkdir("C:\\my_folder")
>>> os.mkdir("C:\\my_folder")

Traceback (most recent call last):


File "<pyshell#196>", line 1, in <module>
os.mkdir("C:\\my_folder")
WindowsError: [Error 183] Cannot create a file when that file
already exists: 'C:\\my_folder'

So, os.mkdir can't overwrite existing directory. You must check for directory existence first:

>>> if not os.path.exists("C:\\my_folder"):


os.mkdir("C:\\my_folder")

os.mkdir can create subdirs only if parent dir already exists:

>>> os.mkdir("C:\\my_folder\\sub1\\sub2")
Traceback (most recent call last):
File "<pyshell#202>", line 1, in <module>
os.mkdir("C:\\my_folder\\sub1\\sub2")
WindowsError: [Error 3] The system cannot find the path
specified: 'C:\\my_folder\\sub1\\sub2'

10 / 43
le system os.makedirs can create missing intermediate directories:

>>> os.makedirs("C:\\my_folder\\sub1\\sub2")
>>>
makedirs >>> os.path.exists("C:\\my_folder\\sub1\\sub2")
True
>>> os.makedirs("C:\\my_folder\\sub1\\sub2")
Traceback (most recent call last):
File "<pyshell#206>", line 1, in <module>
os.makedirs("C:\\my_folder\\sub1\\sub2")
File "C:\Program Files (x86)\Python 2.7.9\lib\os.py", line 157,
in makedirs
mkdir(name, mode)
WindowsError: [Error 183] Cannot create a file when that
file already exists: 'C:\\my_folder\\sub1\\sub2'

11 / 43
le system Create following directory structure:

C:\python_course\temp_01
C:\python_course\temp_02
makedirs and so on... until
C:\python_course\temp_30

Exercise Iterate over content of directory "C:\Boot" and print content of each subdirectory who's name starts with
"zh"

12 / 43
le system >>> import os
>>>
>>> for i in range(1, 31):
os.makedirs("C:\\python_course\\temp_" + str(i).zfill(2))
makedirs >>> sorted(glob.glob("C:\\python_course\\temp_*"))[:3]
['C:\\python_course\\temp_01',
'C:\\python_course\\temp_02',
Solution 'C:\\python_course\\temp_03']

Using os.listdir:

>>> import os
>>> root = "C:\\Boot"
>>>
>>> for dir in os.listdir(root):
full_path = os.path.join(root, dir)
if os.path.isdir(full_path):
if dir.startswith('zh'):
print full_path
print os.listdir(full_path)

C:\Boot\zh-CN
['bootmgr.exe.mui']
C:\Boot\zh-HK
['bootmgr.exe.mui']
C:\Boot\zh-TW
['bootmgr.exe.mui']

13 / 43
Solution Using glob.glob():

>>> import glob


>>> glob.glob("C:\\Boot\\zh*\\*")
>>>
['C:\\Boot\\zh-CN\\bootmgr.exe.mui',
'C:\\Boot\\zh-HK\\bootmgr.exe.mui',
'C:\\Boot\\zh-TW\\bootmgr.exe.mui']

Using os.walk():

>>> import os
>>> for root, dirs, files in os.walk("C:\\Boot"):
if os.path.basename(root).startswith("zh"):
print files
['bootmgr.exe.mui']
['bootmgr.exe.mui']
['bootmgr.exe.mui']

14 / 43
le system Module shutil does file and directory copying/moving:

>>> import shutil


>>>
high level le >>> # first create manually "C:\\my_folder\\my_file.txt"
>>> # in next chapter,
operations >>>
>>>
# we will learn how to do that in a programmatic manner
>>> shutil.copy("C:\\my_folder\\my_file.txt",
"C:\\python_course\\new_file.txt")
>>>
>>> shutil.copy("C:\\my_folder\\my_file.txt", "C:\\python_course")

>>> import glob


>>> glob.glob("C:\\python_course\\*.txt")
['C:\\python_course\\my_file.txt',
'C:\\python_course\\new_file.txt']

shutil.copy2 is the same as shutil.copy but copies metadata too:

>>> shutil.copy2("C:\\my_folder\\my_file.txt",
"C:\\python_course\\new_file_meta.txt")

>>> import os
>>> import time
>>>
>>> "last modified: %s" % time.ctime(os.path.getmtime(
"C:\\python_course\\new_file.txt"))
'last modified: Sun Aug 30 13:16:58 2015'
>>> "last modified: %s" % time.ctime(os.path.getmtime(
"C:\\python_course\\new_file_meta.txt"))
'last modified: Sun Aug 30 13:16:58 2015'
15 / 43
le system Module shutil directory operations:

>>> shutil.copytree("C:\\my_folder", "C:\\my_dir\\sub1")


>>> shutil.move("C:\\my_folder", "C:\\my_dir\\sub2")
high level le >>>
>>> glob.glob("C:\\my_folder")
operations []

>>> glob.glob("C:\\my_dir\\*")
['C:\\my_dir\\my_file.txt', 'C:\\my_dir\\sub1', 'C:\\my_dir\\sub2']

>>> shutil.rmtree("C:\\my_dir\\sub2")
>>>
>>> glob.glob("C:\\my_dir\\*")
['C:\\my_dir\\my_file.txt', 'C:\\my_dir\\sub1']

Where is the file remove operation?

>>> import os
>>> os.remove("C:\\my_dir\\my_file.txt")
>>>
>>> glob.glob("C:\\my_dir\\*")
['C:\\my_dir\\sub1']

16 / 43
le system Copy directory "C:\python_course" to "C:\python_course2"

Rename (move) "C:\python_course2" to "C:\python_course3"

high level le Delete "C:\python_course3"


operations

Exercise

17 / 43
le system >>> import shutil
>>> shutil.copytree("C:\\python_course", "C:\\python_course2")

high level le >>> shutil.move("C:\\python_course2", "C:\\python_course3")

operations >>> import glob


>>> glob.glob("C:\\python_course*")
['C:\\python_course', 'C:\\python_course3']
Solution
>>> shutil.rmtree("C:\\python_course3")

>>> glob.glob("C:\\python_course*")
['C:\\python_course']

18 / 43
les Working with files generally involves three steps:

open file for reading or writing


do something with file
reading close file

>>> f = open('C:\\my_dir\\simic.txt', 'r')


>>> f.read()
'Covjece pazi\nda ne ides malen\nispod zvijezda!\n\nPusti\nda cijelog
tebe prodje\nblaga svjetlost zvijezda!\n\nDa ni za cim ne zalis\nkad
se budes zadnjim pogledima\nrastajao od zvijezda!\n\nNa svom
koncu\nmjesto u prah\nprijedji sav u zvijezde!\n'
>>> f.close()

But if we don't save file handle to a variable, closing is done automatically:

>>> print open('C:\\my_dir\\simic.txt', 'r').read()


Covjece pazi
da ne ides malen
ispod zvijezda!

Pusti
da cijelog tebe prodje
blaga svjetlost zvijezda!
Da ni za cim ne zalis
kad se budes zadnjim pogledima
rastajao od zvijezda!
Na svom koncu
mjesto u prah
prijedji sav u zvijezde!
19 / 43
les File object allows easy looping:

>>> for line in open('C:\\my_dir\\simic.txt', 'r'):


print line,
reading
Covjece pazi
da ne ides malen
ispod zvijezda!
Pusti
da cijelog tebe prodje
blaga svjetlost zvijezda!
Da ni za cim ne zalis
kad se budes zadnjim pogledima
rastajao od zvijezda!
Na svom koncu
mjesto u prah
prijedji sav u zvijezde!

If you wish to count lines, just use enumerate:

>>> for i, line in enumerate(open('C:\\my_dir\\simic.txt', 'r')):


print i, line,
0 Covjece pazi
1 da ne ides malen
2 ispod zvijezda!
3
4 Pusti
5 da cijelog tebe prodje
6 blaga svjetlost zvijezda!
7
8 Da ni za cim ne zalis 20 / 43
9 kad se budes zadnjim pogledima
les Reading the same file object twice:

>>> f = open('C:\\my_dir\\simic.txt', 'r')


>>> print f.read() # works
reading Covjece pazi ...
>>> print f.read() # doesn't work!
>>>

Read operation keeps track of position in file buffer. First call to read() consumes whole file, so there is nothing
left to read in the second call. This can be avoided in two ways:

a) close and re-open file

>>> f = open('C:\\my_dir\\simic.txt', 'r')


>>> print f.read() # works
>>> f.close()
>>> f = open('C:\\my_dir\\simic.txt', 'r')
>>> print f.read() # works
>>> f.close()

b) seek to start of file

>>> f = open('C:\\my_dir\\simic.txt', 'r')


>>> print f.read() # works
>>> f.seek(0)
>>> print f.read() # works
>>> f.close()

21 / 43
les Reading in fixed size buffers:

.read(size) method takes an optional size argument. If size is specified, read operation will return at most size
characters.
reading
>>> f = open('C:\\my_dir\\simic.txt', 'r')
>>> f.read(30)
'Covjece pazi\nda ne ides malen\n'
>>> f.read(30)
'ispod zvijezda!\n\nPusti\nda cije'
>>> f.read(30)
'log tebe prodje\nblaga svjetlos'
>>> f.read(30)
't zvijezda!\n\nDa ni za cim ne z'
>>> f.read(30)
'alis\nkad se budes zadnjim pogl'
>>> f.read(30)
'edima\nrastajao od zvijezda!\n\nN'
>>> f.read(30)
'a svom koncu\nmjesto u prah\npri'
>>> f.read(30)
'jedji sav u zvijezde!\n'
>>> f.read(30)
''

22 / 43
les Reading in fixed size buffers:

.read(size) method takes an optional size argument. If size is specified, read operation will return at most size
characters.
reading
>>> f = open('C:\\my_dir\\simic.txt', 'r')
>>> f.read(30)
'Covjece pazi\nda ne ides malen\n'
>>> f.read(30)
'ispod zvijezda!\n\nPusti\nda cije'
>>> f.read(30)
'log tebe prodje\nblaga svjetlos'
>>> f.read(30)
't zvijezda!\n\nDa ni za cim ne z'
>>> f.read(30)
'alis\nkad se budes zadnjim pogl'
>>> f.read(30)
'edima\nrastajao od zvijezda!\n\nN'
>>> f.read(30)
'a svom koncu\nmjesto u prah\npri'
>>> f.read(30)
'jedji sav u zvijezde!\n'
>>> f.read(30)
''

23 / 43
les File object allows easy reading and writing of strings:

>>> simic = open('C:\\my_dir\\simic.txt', 'r').read()


>>>
writing >>> new = open('C:\\my_dir\\new.txt', 'w')
>>> new.write(simic)
>>> new.close()
>>>
>>> print open('C:\\my_dir\\new.txt', 'r').read()
Covjece pazi
da ne ides malen
ispod zvijezda!
Pusti
da cijelog tebe prodje
blaga svjetlost zvijezda!
Da ni za cim ne zalis
kad se budes zadnjim pogledima
rastajao od zvijezda!

Na svom koncu
mjesto u prah
prijedji sav u zvijezde!

24 / 43
les File object also allows easy reading and writing of lists:

>>> simic = open('C:\\my_dir\\simic.txt', 'r').readlines()


>>> simic
writing ['Covjece pazi\n', 'da ne ides malen\n', 'ispod zvijezda!\n',
'\n', 'Pusti\n', 'da cijelog tebe prodje\n',
'blaga svjetlost zvijezda!\n', '\n', 'Da ni za cim ne zalis\n',
'kad se budes zadnjim pogledima\n', 'rastajao od zvijezda!\n',
'\n', 'Na svom koncu\n', 'mjesto u prah\n',
'prijedji sav u zvijezde!\n']

>>> new = open('C:\\my_dir\\new2.txt', 'w')


>>> new.writelines(simic)
>>> new.close()
>>>
>>> print open('C:\\my_dir\\new2.txt', 'r').read()
Covjece pazi
da ne ides malen
ispod zvijezda!

Pusti
da cijelog tebe prodje
blaga svjetlost zvijezda!
Da ni za cim ne zalis
kad se budes zadnjim pogledima
rastajao od zvijezda!
Na svom koncu
mjesto u prah
prijedji sav u zvijezde!

25 / 43
les r
Opens a file for reading only.
The file pointer is placed at the beginning of the file. This is the default mode.
rb
writing Opens a file for reading only in binary format.
The file pointer is placed at the beginning of the file. This is the default mode.
r+
Opens a file for both reading and writing.
The file pointer placed at the beginning of the file.
rb+
Opens a file for both reading and writing in binary format.
The file pointer placed at the beginning of the file.
w
Opens a file for writing only.
Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
wb
Opens a file for writing only in binary format.
Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
w+
Opens a file for both writing and reading.
Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading
and writing.

26 / 43
les wb+
Opens a file for both writing and reading in binary format.
Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading
and writing.
writing a
Opens a file for appending.
The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the
file does not exist, it creates a new file for writing.
ab
Opens a file for appending in binary format.
The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the
file does not exist, it creates a new file for writing.
a+
Opens a file for both appending and reading.
The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file
does not exist, it creates a new file for reading and writing.
ab+
Opens a file for both appending and reading in binary format.
The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file
does not exist, it creates a new file for reading and writing.

27 / 43
les Sometimes it is really useful to save python objects to (and load from) hard drive:

>>> import cPickle


>>>
cPickle >>> f = open("C:\\python_course\\pickle.txt", "w")
>>> L = list("abcd")
>>> L
['a', 'b', 'c', 'd']

>>> cPickle.dump(L, f)
>>> f.close()

>>> print open("C:\\python_course\\pickle.txt", "r").read()


(lp1
S'a'
aS'b'
aS'c'
aS'd'
a.

>>> f = open("C:\\python_course\\pickle.txt", "r")


>>> L2 = cPickle.load(f)
>>> f.close()
>>> L2
['a', 'b', 'c', 'd']
>>> L2.append('e')
>>> L2
['a', 'b', 'c', 'd', 'e']

28 / 43
les Python has a built-in support for .csv (comma separated values) format:

>>> import string


>>> import csv
csv >>>
>>> # on MS Windows, make sure you open file in binary mode
>>> f = open("C:\\python_course\\test_writer.csv", "wb")
>>>
>>> for i in range(10):
L = []
L.append(i)
L.append(string.ascii_lowercase[i])
L.append(2000 + i)
csv.writer(f).writerow(L)

>>> f.close()

>>> print open("C:\\python_course\\test_writer.csv", "r").read()


0,a,2000
1,b,2001
2,c,2002
3,d,2003
4,e,2004
5,f,2005
6,g,2006
7,h,2007
8,i,2008
9,j,2009

29 / 43
les Reading from .csv files:

>>> import csv


>>> f = open("C:\\python_course\\ufc_hw_champions.csv", "r")
csv >>>
>>> for row in csv.reader(f, delimiter=','):
print row
['1', 'Mark Coleman', '1997']
['2', 'Maurice Smith', '1997']
['3', 'Randy Couture', '1997']
['4', 'Bas Rutten', '1999']
['5', 'Kevin Randleman', '1999']
['6', 'Randy Couture', '2000']
['7', 'Josh Barnett', '2002']
['8', 'Ricco Rodriguez', '2002']
['9', 'Tim Sylvia', '2003']
['10', 'Andrei Arlovski', '2005']
['11', 'Tim Sylvia', '2006']
['12', 'Randy Couture', '2007']
['13', 'Brock Lesnar', '2008']
['14', 'Cain Velasquez', '2010']
['15', 'Junior dos Santos', '2011']
['16', 'Cain Velasquez', '2012']
['17', 'Fabricio Werdum', '2015']
['18', 'Stipe Miocic', '2016']
['19', 'Daniel Cormier', '2018']
>>> f.close()

Delimiter is by default already ',' so we may omit it.

But when you use CSV files generated by MS Excel, delimiter will be a semicolon ';' and then you need to set
it explicitly.
30 / 43
Exercise Write all lower case letters of english alphabet into a file "alphabet.txt". Put each letter into a separate line.

Append at the end of the same file ("alphabet.txt"): numbers 1-100, also one number per line

Read the content of the file, and print every 10th line to the screen

31 / 43
Solution >>> import string
>>> f = open("C:\\python_course\\alphabet.txt", "w")
>>> for ch in string.ascii_lowercase:
f.write(ch + '\n')
>>> f.close()

>>> f = open("C:\\python_course\\alphabet.txt", "a")


>>> for i in range(1, 101):
f.write(str(i) + '\n')
>>> f.close()

>>> for i, line in enumerate(open("C:\\python_course\\alphabet.txt")):


if i % 10 == 0:
print line,
a
k
u
5
15
25
35
45
55
65
75
85
95

32 / 43
Exercise Copy "exercise_2.csv" to your local folder.

Using "csv.reader" and "csv.writer" objects:

read "exercise_2.csv" and print each line as list object

add another column in "exercise_2.csv" file.


New column should contain numbers starting with 100.
Save result to file "exercise_2_new.csv"

33 / 43
Solution >>>
>>>
f1 = open("C:\\python_course\\exercise_2.csv", "r")
f2 = open("C:\\python_course\\exercise_2_new.csv", "wb")
>>>
>>> writer = csv.writer(f2)
>>>
>>> for i, row in enumerate(csv.reader(f1)):
row.append(100 + i)
writer.writerow(row)

>>> f1.close()
>>> f2.close()

>>> f = open("C:\\python_course\\exercise_2_new.csv", "r")


>>> print f.read()
1,Mark Coleman,1997,100
2,Maurice Smith,1997,101
3,Randy Couture,1997,102
4,Bas Rutten,1999,103
5,Kevin Randleman,1999,104
6,Randy Couture,2000,105
7,Josh Barnett,2002,106
8,Ricco Rodriguez,2002,107
9,Tim Sylvia,2003,108
10,Andrei Arlovski,2005,109
11,Tim Sylvia,2006,110
12,Randy Couture,2007,111
13,Brock Lesnar,2008,112
14,Cain Velasquez,2010,113
15,Junior dos Santos,2011,114
16,Cain Velasquez,2012,115
17,Fabricio Werdum,2015,116
18,Stipe Miocic,2016,117
>>> f.close()

34 / 43
Exercise Copy "exercise_3.log" log file to your local folder.
Sample line from log file:

sig18 0x1e 0x5b 0x14a1 0x29 0x352 0x31 0x83 0x1c2 0x3bb

Find all lines that have string "sig18" in the first column.

read 9th column


convert value to decimal
print it to the screen

35 / 43
Solution >>> for line in open("C:\\python_course\\exercise_3.log"):
parts = line.split()
if parts[0] == "sig18 ":
print int(parts[-2], 16)
450
450
450
450
450
450
450
450
450
451
450

36 / 43
Exercise Copy "exercise_4.log" log file to your local folder.

Extract all H3RPCSSENDE<36> signals that had D5 field set to "d2":

solve:
by using regex
by parsing file line by line
save result to file "exercise_4_short.log"

Example of signal definition that we are looking for:

Event: Send H3RPCSSENDE<36>


From: H3STHR:H3STHR
To: RAZOR:RP_CP
0004: OWN_REF = 00 00 00 01
0008: ecode = 00 00 00 31
000c: D1 = 00 00 00 00
0010: D2 = 00 00 00 00
0014: D3 = 00 00 00 00
0018: D4 = 00 00 00 00
001c: D5 = 00 00 00 d2
0020: D6 = 00 00 00 00
0024: D7 = 00 00 00 00
0028: D8 = 00 00 00 4d

37 / 43
Solution >>>
>>>
import re
>>> pattern = ".{12}H3RPCSSENDE<36>[^<]*?D5.{12}d2.*?0028.{18}"
>>>
>>> raw = open("C:\\python_course\\exercise_4.log").read()
>>> results = re.findall(pattern, raw, flags=re.DOTALL)
>>>
>>> len(results)
88
>>> print results[0]
Event: Send H3RPCSSENDE<36>
From: H3STHR:H3STHR
To: RAZOR:RP_CP
0004: OWN_REF = 00 00 00 01
0008: ecode = 00 00 00 31
000c: D1 = 00 00 00 00
0010: D2 = 00 00 00 00
0014: D3 = 00 00 00 00
0018: D4 = 00 00 00 00
001c: D5 = 00 00 00 d2
0020: D6 = 00 00 00 00
0024: D7 = 00 00 00 00
0028: D8 = 00 00 00 4c

38 / 43
Solution >>>
>>>
signals = []
signal = ""
>>> in_signal = False
>>> is_d2 = False

>>> for line in open("C:\\python_course\\exercise_4.log"):


if line.startswith("Event: Send H3RPCSSENDE<36>"):
in_signal = True
is_d2 = False
if line == "\n":
in_signal = False
if is_d2 == True:
signals.append(signal)
signal = ""
is_d2 = False
if in_signal == True:
signal += line
parts = line.split()
if parts[1] == "D5" and parts[6] == "d2":
is_d2 = True
>>> len(signals)
88
>>> print signals[0]
Event: Send H3RPCSSENDE<36>
From: H3STHR:H3STHR
To: RAZOR:RP_CP
0004: OWN_REF = 00 00 00 01
0008: ecode = 00 00 00 31
000c: D1 = 00 00 00 00
0010: D2 = 00 00 00 00
0014: D3 = 00 00 00 00
0018: D4 = 00 00 00 00
001c: D5 = 00 00 00 d2
0020: D6 = 00 00 00 00
0024: D7 = 00 00 00 00 39 / 43
0028: D8 = 00 00 00 4c
Solution >>> original = open("C:\\python_course\\exercise_4.log").read()
>>>
>>> signals = []
>>>
>>> for sig in original.split("\n\n"):
if "<36>" in sig and "D5 = 00 00 00 d2" in sig:
signals.append(sig)

>>> len(signals)
88
>>> print signals[0]
(140) Time: 21000.787 ms
Event: Send H3RPCSSENDE<36>
From: H3STHR:H3STHR
To: RAZOR:RP_CP
0004: OWN_REF = 00 00 00 01
0008: ecode = 00 00 00 31
000c: D1 = 00 00 00 00
0010: D2 = 00 00 00 00
0014: D3 = 00 00 00 00
0018: D4 = 00 00 00 00
001c: D5 = 00 00 00 d2
0020: D6 = 00 00 00 00
0024: D7 = 00 00 00 00
0028: D8 = 00 00 00 4c

40 / 43
Exercise Copy "exercise_5.script" log file to your local folder.

Update all lines that start with "TEST CASE ID:" to include order number of test case, starting with
number 011. Something like this:

TEST CASE ID: 011


TEST CASE ID: 012
TEST CASE ID: 013
TEST CASE ID: 014
TEST CASE ID: 015
TEST CASE ID: 016
TEST CASE ID: 017
TEST CASE ID: 018
TEST CASE ID: 019
TEST CASE ID: 020
TEST CASE ID: 021
and so on ...

41 / 43
Solution >>> i = 11
>>> f = open("C:\\python_course\\exercise_5_new.script", "w")
>>> for line in open("C:\\python_course\\exercise_5.script"):
if line.startswith("TEST CASE ID:"):
f.write("TEST CASE ID: %s\n" % str(i).zfill(3))
i += 1
else:
f.write(line)
>>> f.close()

>>> import re
>>> raw = open("C:\\python_course\\exercise_5_new.script").read()
>>>
>>> pattern = "TEST CASE ID:.*"
>>>
>>> results = re.findall(pattern, raw)
>>> for result in results:
print result

TEST CASE ID: 011


TEST CASE ID: 012
TEST CASE ID: 013
TEST CASE ID: 014
TEST CASE ID: 015
TEST CASE ID: 016
TEST CASE ID: 017
TEST CASE ID: 018
TEST CASE ID: 019
TEST CASE ID: 020
TEST CASE ID: 021
TEST CASE ID: 022
TEST CASE ID: 023
TEST CASE ID: 024
TEST CASE ID: 025
TEST CASE ID: 026
TEST CASE ID: 027 42 / 43
TEST CASE ID: 028
That's all folks
It's up to you to continue exploring Python

-- Happy coding --

43 / 43

You might also like