Basics of scripting in Python
31-Jan-2026
Why scripting in VLSI?
• Automation in RTL, verification
• Parsing reports, logs, netlists
File Handling
File handling refers to the process of performing operations on a
file, such as creating, opening, reading, writing and closing it
through a programming interface
Opening and closing a file
• Opening of file is done using open() function
• open() takes two arguments:
• filepath
• mode
• Different modes are possible – “r”, “w”, “a”, “x”, “t”, “b”
• Example: file = open(“pd_hw1.txt”, “r”)
• Closing of file is done using close() function
• Example: [Link]()
Opening and closing a file
• Need to close the file after file manipulation
• Can use with to open a file which closes the file automatically
after the operations are done
• Example:
with open(“pd_hw1.txt", "r") as file:
print([Link]())
Reading a file
• using read() function
Example:
print([Link]())
• Can read lines in a file using loop
Example:
with open(“pd_hw1.txt") as f:
for line in f:
print(line)
Writing to a file
• To write to an existing file, you must add a parameter to
the open() function:
• "a" - Append - will append to the end of the file
• "w" - Write - will overwrite any existing content
• New file is created if the file does not exist.
Example:
with open(“pd_hw1.txt", "a") as f:
[Link](“I am the new line!")
Creating and deleting a file
• mode “x” is used to create a file – returns error if the file already exists
• Deleting files and folders is done using functions from “os” library.
Example:
import os
if [Link]("[Link]"):
[Link]("[Link]")
else:
print("The file does not exist")
Regular Expressions
• used to search, match, extract, and manipulate text patterns.
Character classes:
any character Any lowercase
. [a-z]
except newline alphabet
word, digit, Any uppercase
\w \d \s [A-Z]
whitespace alphabet
not word, digit, Any digit from 0 to
\W \D \S [0-9] or \d
whitespace 9
any alphanumeric
[abc] any of a, b, or c [a-zA-Z0-9]
character
[^abc] not a, b, or c [^abc] not a, b, or c
character between Any special
[a-g] [^a-zA-Z0-9]
a&g character
Regular Expressions
Quantifiers and alternation:
0 or more, 1 or
a* a+ a?
more, 0 or 1
exactly five, two
a{5} a{2,}
or more
between one &
a{1,3}
three
match as few as
a+? a{2,}? non greedy
possible
ab|cd match ab or cd
Regular Expressions
Anchors, Groups and Escaped characters:
Start/end of the
^abc$
string
Word, not-word
\b \B
boundary
Escaped special
\. \* \\
characters
(abc) Capture group
RegEx in Python
• Python uses the re module for regex.
import re
• Basic regex functions in Python in the next slides
[Link]
• Finds first match
Code:
import re
text = “First PD Tutorial today"
match = [Link](“First", text)
print([Link]())
Output:
First
[Link]
• Find all matches
Code:
import re
text = “red fed bed"
match = [Link](“ed", text)
print(match())
Output:
[‘ed’, ‘ed’, ‘ed’]
More functions for you to explore
• [Link]() - match only at start
• [Link]() - split using regex
• [Link]() - replace text
• [Link]() - all matches
Example: Timing report
Goal: To find the worst slack
Slack = -0.23 ns
Slack = 0.15 ns
Slack = -0.05 ns
Example: Timing report
Code:
import re
slacks = []
with open("[Link]") as f:
for line in f:
m = [Link](r"Slack\s*=\s*(-?\d+\.\d+)", line)
if m:
[Link](float([Link](1)))
print("Worst Slack:", min(slacks))
References
• [Link]
• [Link]
• [Link]
• [Link]