ãã¡ã¤ã«ãéããèªã¿è¾¼ããæ¸ãè¾¼ã
f1 = f2 = None
try:
f1 = open("in.txt", "r")
f2 = open("out.txt", "w")
data = f1.read(100)
data += f1.read()
f2.write(data)
except IOError, inst:
print inst
finally:
if f1: f1.close()
if f2: f2.close()
ãã¡ã¤ã«ã1è¡ãã¤èªã¿è¾¼ã ãã®1
f = open("in.txt", "r")
for line in f:
print line,
f.close()
ãã¡ã¤ã«ã1è¡ãã¤èªã¿è¾¼ã ãã®2
f = open("in.txt")
for i in xrange(10):
line = f.readline()
if line:
print i + 1, line
else:
break
f.close()
ãªã¹ãã«èªã¿è¾¼ãããªã¹ããæ¸ãè¾¼ã
f = open("in.txt", "r+")
lines = f.readlines()
lines.sort(key=lambda x:x.split(",")[0])
f.seek(0)
f.writelines(lines)
f.close()