Jan 1, 2018

[Python] an example of I/O, file I/O

a = float(input('enter any number: '))
print('%f * %f = %f\n' % (a, a, a*a))


a,b = input('enter any two numbers: ').split()  # split(): two numbers with space in between;  split(','): two numbers with , in between
print('%f * %f = %f\n' % (float(a), float(b), float(a)*float(b)))


now = time.localtime()
filename = "log_%02d%02d%02d_%02d:%02d:%02d.txt" % (now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec)
f = open(filename, 'w')  # r: read-only,  r+: read and write, w: write,  a: append
f.write('abcdefgh')
f.close()


g = open('t.txt', 'r')
line = g.readline()
while line:
 print (line, end='\n')
 line = g.readline()
g.close()


h = open('t.txt', 'r')
lines = h.readlines()
for line in lines:
 print(line*2, end=' ')
h.close()


Result------------------------------------------
enter any number: 1.000000 * 1.000000 = 1.000000

enter any two numbers: 1.000000 * 2.000000 = 2.000000

abcdefgh
abcdefghabcdefgh 

No comments:

Post a Comment