ProgBasic2 File
ProgBasic2 File
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
test.txt
File open
• open .
open( , mode )
“test.txt” “r”
“out.dat” “w”
• mode “lina.jpg” “a”
• “r” : read, “w” : write, “a” : append
• return
• f = open(“test.txt”,”r”)
“r”
File
• “r” open
• f_in = open(“test.txt”,”r”)
• .
• read()
• readline()
• readlines()
for le
• –
fi
• open()
“ ”
• open( )
• r
• w
• w+
• a
• write( )
• ( )
• le
fi
(Flushing)
•
•
( )
for x in open(”test.txt”):
print(x)
OR
f = open(”test.txt”,”r”):
for x in f.readlines():
print(x)
( )
t = open(”output.txt”,”w”)
for x in data_list:
t.write(x)
t.close()
( )
t = open(”output.txt”,”w”)
for x in open(”input.txt”):
s = x.split()
t.write(s[0])
t.close()
CSV
• Comma-Separated Values
• ‘,’ 123,56,12412,102
842,12,341,921
91,8402,23,34412
199,2004,100,200
( 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("data.csv","r"):
... t = line.split(",")
... print(t[1])
... num.append( float( t[1] ) )
...
56
12
8402
2004
>>> num
[56.0, 12.0, 8402.0, 2004.0]
>>> sum(num)
10474.0