.
File
memory hdd/ssd
Variables
Files
Python
Program
File
• string
• Line ‘\n’
• Python open .
I am a boy
second I am a boy\nsecond\nthird
third
[Link]
File open
• open .
open( , mode )
“[Link]” “r”
“[Link]” “w”
• mode “[Link]” “a”
• “r” : read, “w” : write, “a” : append
• return
• f = open(“[Link]”,”r”)
“r”
File
• “r” open
• f_in = open(“[Link]”,”r”)
• .
• read()
• readline()
• readlines()
for le
• –
fi
• open()
“ ”
• open( )
• r
• w
• w+
• a
• write( )
out = open(“[Link]”, “w”)
[Link](“Hello! \n”)
• print( )
• ( )
• le
fi
(Flushing)
•
•
( )
for x in open(”[Link]”):
print(x)
OR
f = open(”[Link]”,”r”):
for x in [Link]():
print(x)
( )
t = open(”[Link]”,”w”)
for x in data_list:
[Link](x)
[Link]()
( )
t = open(”[Link]”,”w”)
for x in open(”[Link]”):
s = [Link]()
[Link](s[0])
[Link]()
CSV
• Comma-Separated Values
• ‘,’ 123,56,12412,102
842,12,341,921
91,8402,23,34412
199,2004,100,200
123, 56, 12412, 102\n
842, 12, 341, 921\n
4 rows 91, 8402, 23, 34412\n
199, 2004, 100, 200\n
4 columns
CSV
• (read) ‘,’ (split) .
>>> for line in open("[Link]","r"):
... t = [Link](",")
... print(t[1])
...
56
12
8402
2004
(integer)
( oating-point
numbers)
(string)
fl
(integer)
str()
int() oat()
str()
int()
( oating-point
numbers)
(string)
oat()
fl
fl
fl
CSV
>>> num = []
>>> for line in open("[Link]","r"):
... t = [Link](",")
... print(t[1])
... [Link]( float( t[1] ) )
...
56
12
8402
2004
>>> num
[56.0, 12.0, 8402.0, 2004.0]
>>> sum(num)
10474.0