Python Interviews: Difference between revisions
→"is" vs "=="
Tag: Reverted |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 6:
= Open a file =
f = open('text.txt','r')
l = f.readlines()
f.close()
for i in l:
print(i.strip('\n'))
= Read Log File =▼
* Better way to open a file as it auto closed file, even on crash
<pre>▼
with open('/var/log/apache2/access.log','r') as file:▼
for line in file:▼
print(line.split()[3], line.split()[6], line.split()[8], line.split()[9])▼
try:▼
b = b +int(line.split()[9])▼
except:▼
pass▼
print(b)▼
</pre>▼
= List Comprehension =
Line 278 ⟶ 295:
False
▲= Read Log File =
▲<pre>
▲with open('/var/log/apache2/access.log','r') as file:
▲ for line in file:
▲ print(line.split()[3], line.split()[6], line.split()[8], line.split()[9])
▲ try:
▲ b = b +int(line.split()[9])
▲ except:
▲ pass
= Dictionary =
▲print(b)
▲</pre>
a = {}
|