Python Course
Python Course
Python Course
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']
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:
and directories:
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).
*
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:
>>> os.path.basename(root)
'my_file.txt'
>>> os.path.dirname(root)
'C:\\my_folder'
>>> os.path.split(root)
('C:\\my_folder', 'my_file.txt')
>>> os.path.splitext(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.
8 / 43
le system: dir If you wish to skip some subdirectories, just remove them from dirs list:
So, os.mkdir can't overwrite existing directory. You must check for directory existence first:
>>> 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():
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:
>>> 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:
>>> 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']
>>> 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"
Exercise
17 / 43
le system >>> import shutil
>>> shutil.copytree("C:\\python_course", "C:\\python_course2")
>>> glob.glob("C:\\python_course*")
['C:\\python_course']
18 / 43
les Working with files generally involves three steps:
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:
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:
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:
Na svom koncu
mjesto u prah
prijedji sav u zvijezde!
24 / 43
les File object also allows easy reading and writing of lists:
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:
>>> cPickle.dump(L, f)
>>> f.close()
28 / 43
les Python has a built-in support for .csv (comma separated values) format:
>>> f.close()
29 / 43
les Reading from .csv files:
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()
32 / 43
Exercise Copy "exercise_2.csv" to your local folder.
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()
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.
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.
solve:
by using regex
by parsing file line by line
save result to file "exercise_4_short.log"
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
>>> 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:
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
-- Happy coding --
43 / 43