For today’s practice make use of notebook module2-day2.ipynb created in your enviroment. Shut down kernel for all previous notebooks (if in runing condition) by right cliking on notbeook on left hand side file browser.
13.1 Reading files
!cat poem.txt
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
withopen("poem.txt") as filehandle: text = filehandle.read()
print(text)
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
withopen("poem.txt") as f:print(f.readline(), end="")
for line in f: # this loop will readinf file from where the erlier poiter left!print(line, end="")
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
f.close()
def printfile_with_linunums(filename):withopen(filename) as f:for linenum, line inenumerate(f, start=1):print(linenum, line)
printfile_with_linunums("poem.txt")
1 The Zen of Python, by Tim Peters
2
3 Beautiful is better than ugly.
4 Explicit is better than implicit.
5 Simple is better than complex.
6 Complex is better than complicated.
7 Flat is better than nested.
8 Sparse is better than dense.
9 Readability counts.
10 Special cases aren't special enough to break the rules.
11 Although practicality beats purity.
12 Errors should never pass silently.
13 Unless explicitly silenced.
14 In the face of ambiguity, refuse the temptation to guess.
15 There should be one-- and preferably only one --obvious way to do it.
16 Although that way may not be obvious at first unless you're Dutch.
17 Now is better than never.
18 Although never is often better than *right* now.
19 If the implementation is hard to explain, it's a bad idea.
20 If the implementation is easy to explain, it may be a good idea.
21 Namespaces are one honking great idea -- let's do more of those!
def printfile_with_linunums(filename):withopen(filename) as f:for linenum, line inenumerate(f, start=1):print(linenum, line, end="")
printfile_with_linunums("poem.txt")
1 The Zen of Python, by Tim Peters
2
3 Beautiful is better than ugly.
4 Explicit is better than implicit.
5 Simple is better than complex.
6 Complex is better than complicated.
7 Flat is better than nested.
8 Sparse is better than dense.
9 Readability counts.
10 Special cases aren't special enough to break the rules.
11 Although practicality beats purity.
12 Errors should never pass silently.
13 Unless explicitly silenced.
14 In the face of ambiguity, refuse the temptation to guess.
15 There should be one-- and preferably only one --obvious way to do it.
16 Although that way may not be obvious at first unless you're Dutch.
17 Now is better than never.
18 Although never is often better than *right* now.
19 If the implementation is hard to explain, it's a bad idea.
20 If the implementation is easy to explain, it may be a good idea.
21 Namespaces are one honking great idea -- let's do more of those!
%load_problem cat
Problem: Cat
Write a program cat.py that takes one or more filenames are command-line arguments and prints the contents of them.
$ python cat.py files/five.txt
one
two
three
four
five
$ python cat.py files/abcd.txt files/1234.txt
A
B
C
D
1
2
3
4
Write a program grep.py that takes a pattern and a file as command-line arguments and print all the lines in the file that contain that pattern.
The pattern could be any text and there is no need to support regular expressions.
$ cat files/zen-of-python.txt
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
$ python grep.py never files/zen-of-python.txt
Errors should never pass silently.
Now is better than never.
Although never is often better than *right* now.
$ grep the files/zen-of-python.txt
Special cases aren't special enough to break the rules.
In the face of ambiguity, refuse the temptation to guess.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
You can verify your solution using:
%verify_problem grep
%%file grep.py# your code here
!python args.py poem.txt hello.txt
['args.py', 'poem.txt', 'hello.txt']
args = ['args.py', 'poem.txt', 'hello.txt']
str(args)
"['args.py', 'poem.txt', 'hello.txt']"
args
['args.py', 'poem.txt', 'hello.txt']
for f in args:print(f)
args.py
poem.txt
hello.txt
%%file a_cat.py# your code hereimport sysdef print_file(file):withopen(file) as f:for i,l inenumerate(f,start=1):print(i,l,end="")def print_files(files):forfilein files: print_file(file)k = sys.argv[1:] # multiple filesf = sys.argv[1] # single element print_files(k)
Writing a_cat.py
!python a_cat.py poem.txt hello.txt
1 The Zen of Python, by Tim Peters
2
3 Beautiful is better than ugly.
4 Explicit is better than implicit.
5 Simple is better than complex.
6 Complex is better than complicated.
7 Flat is better than nested.
8 Sparse is better than dense.
9 Readability counts.
10 Special cases aren't special enough to break the rules.
11 Although practicality beats purity.
12 Errors should never pass silently.
13 Unless explicitly silenced.
14 In the face of ambiguity, refuse the temptation to guess.
15 There should be one-- and preferably only one --obvious way to do it.
16 Although that way may not be obvious at first unless you're Dutch.
17 Now is better than never.
18 Although never is often better than *right* now.
19 If the implementation is hard to explain, it's a bad idea.
20 If the implementation is easy to explain, it may be a good idea.
21 Namespaces are one honking great idea -- let's do more of those!
1 hello
2 welcome
3 namaskar
%%file grep.pyimport sysdef word_found(filename,word):withopen (filename) as f: content = f.read()for line in content:if word in line:print(line)word_found(sys.argv[1],sys.argv[2])
13.1.1 different modes to open files
Here are different modes in which file can be opened::
================ ============================================
mode meaning
================ ============================================
no mode given open file in text mode for reading
"w" open file to write. if file exists, overrite
"a" open file to append. if file exists, append
"rb" or "b" open file to read bibary
"wb" open file in write mode to write binary.
"ab" open file in bibary and append mode
================ ============================================
withopen("digits.txt", "w") as f: f.write("one") f.write("two")
!python cat.py digits.txt
onetwo
withopen("digits.txt", "w") as f: f.write("one") f.write("\n") f.write("two")
!python cat.py digits.txt
one
two
withopen("stocks.csv", "w") as f: header =",".join(list(stocks[0].keys())) f.write(header) f.write("\n")for st in stocks: row =",".join(list(st.values())) f.write(row) f.write("\n")
---------------------------------------------------------------------------TypeError Traceback (most recent call last)
CellIn[45], line 6 4 f.write("\n")
5for st in stocks:
----> 6 row = ",".join(list(st.values())) 7 f.write(row)
8 f.write("\n")
TypeError: sequence item 1: expected str instance, int found
",".join([12, 34, 34, 54])
---------------------------------------------------------------------------TypeError Traceback (most recent call last)
CellIn[47], line 1----> 1",".join([12,34,34,54])TypeError: sequence item 0: expected str instance, int found
withopen("stocks.csv", "w") as f: header =",".join(list(stocks[0].keys())) f.write(header) f.write("\n")for st in stocks: row =",".join([str(v) for v in st.values()]) f.write(row) f.write("\n")
%%file multiply_many.pyimport sysdef product(nums): p =1for n in nums: p = p * nreturn pif__name__=="__main__": # dunder nums = [float(n) for n in sys.argv[1:]]print(product(nums))
Overwriting multiply_many.py
!python multiply_many.py 231223124
50784.0
!python multiply_many.py 125
60.0
import multiply_many
!python a_cat.py poem.txt
1 The Zen of Python, by Tim Peters
2
3 Beautiful is better than ugly.
4 Explicit is better than implicit.
5 Simple is better than complex.
6 Complex is better than complicated.
7 Flat is better than nested.
8 Sparse is better than dense.
9 Readability counts.
10 Special cases aren't special enough to break the rules.
11 Although practicality beats purity.
12 Errors should never pass silently.
13 Unless explicitly silenced.
14 In the face of ambiguity, refuse the temptation to guess.
15 There should be one-- and preferably only one --obvious way to do it.
16 Although that way may not be obvious at first unless you're Dutch.
17 Now is better than never.
18 Although never is often better than *right* now.
19 If the implementation is hard to explain, it's a bad idea.
20 If the implementation is easy to explain, it may be a good idea.
21 Namespaces are one honking great idea -- let's do more of those!
import a_cat
---------------------------------------------------------------------------FileNotFoundError Traceback (most recent call last)
CellIn[61], line 1----> 1importa_catFile /opt/arcesium-finop-25/website/live-notes/a_cat.py:18 14 print_file(file)
17 k=sys.argv[1:]
---> 18print_files(k)File /opt/arcesium-finop-25/website/live-notes/a_cat.py:14, in print_files(files) 12defprint_files(files):
13for file in files:
---> 14print_file(file)File /opt/arcesium-finop-25/website/live-notes/a_cat.py:6, in print_file(file) 5defprint_file(file):
----> 6withopen(file)as f:
7for i,l inenumerate(f,start=1):
8print(i,l,end="")
FileNotFoundError: [Errno 2] No such file or directory: '-f'
The process of loading text data into usable python data structure is called as parsing
def parse_csv(csvfile):withopen(csvfile) as f: data = []for line in f: l = line.strip() # remove trailing spaces/new line charecter tokens = l.split(",") data.append(tokens)return data
msg ="""Hi {user},Welcome to the Python Training program!Your login credentials for accessing the lab server (used throughout the training starting Wednesday, 10 Dec 2025) are below:Username: {username}Password: {password}Please use these credentials to log in to the lab environment:Lab Portal: https://lab.finop-25.arcesium.vikrant.dev/You can find the full training schedule, materials, and other details here:Training Website: https://finop-25.arcesium.vikrant.dev/If you have any trouble accessing the portal, feel free to reach out.Regards,Vikrant"""
Hi Vikrant Patil,
Welcome to the Python Training program!
Your login credentials for accessing the lab server (used throughout the training starting Wednesday, 10 Dec 2025) are below:
Username: vikrant
Password: vikrant12231
Please use these credentials to log in to the lab environment:
Lab Portal: https://lab.finop-25.arcesium.vikrant.dev/
You can find the full training schedule, materials, and other details here:
Training Website: https://finop-25.arcesium.vikrant.dev/
If you have any trouble accessing the portal, feel free to reach out.
Regards,
Vikrant
msg
'\nHi {user},\n\nWelcome to the Python Training program!\nYour login credentials for accessing the lab server (used throughout the training starting Wednesday, 10 Dec 2025) are below:\n\nUsername: {username}\nPassword: {password}\n\nPlease use these credentials to log in to the lab environment:\n\nLab Portal: https://lab.finop-25.arcesium.vikrant.dev/\n\nYou can find the full training schedule, materials, and other details here:\n\nTraining Website: https://finop-25.arcesium.vikrant.dev/\n\nIf you have any trouble accessing the portal, feel free to reach out.\n\nRegards,\nVikrant'
"hello the answer to {} of life is {}".format("question", 42)
'hello the answer to question of life is 42'
"I can also have friends like {0} {2} {1}".format("int", "float", "list")
'I can also have friends like int list float'
user ="vikrant"password ="vikealkjfa8eq"f"Hello, this is you username:{user} and password: {password}"
'Hello, this is you username:vikrant and password: vikealkjfa8eq'
def generate_triangle(base, char="*"):return [[char]*i for i inrange(1, base+1)]def pretyprint_tr(triangle): n =len(max(triangle, key=len)) maxsize = n + n-1for line in triangle:print(" ".join(line).center(maxsize))