2/07/2014

(python Study) input/output and file handle (example source code)

__author__ = 'mare'


print(1)
print('hi, guyz')
a = 'hello\n'
print(a)
#1
#hi, guyz
#hello



x=0.2
print(x)
str(x)
print( str(x) )
a = repr('hello\n')
print(a)
#0.2
#0.2
#hello\n



print(x, 'test')
print(a + 'this is test')
#0.2 test
#'hello\n'this is test



import sys
print("welcome","to","python", sep="~", end="!")
#welcome~to~python!
#print("welcome","to","python", sep="~", end="!", file=sys.stderr)
#/Library/Frameworks/Python.framework/Versions/3.3/bin/python3 /Users/mare/PycharmProjects/input_output/example.py
#1
#welcome~to~python!


###########
#alienment
print('\n')
for x in range(1, 6):
 print(x, '*', x, '=', x*x)
#1 * 1 = 1
#2 * 2 = 4
#3 * 3 = 9
#4 * 4 = 16
#5 * 5 = 25

#using rjust for alienment
for x in range(1, 6):
 print(x, '*', x, '=', str(x*x).rjust(4))

#1 * 1 =    1
#2 * 2 =    4
#3 * 3 =    9
#4 * 4 =   16
#5 * 5 =   25

#zfill example
for x in range(1, 6):
 print(x, '*', x, '=', str(x*x).zfill(3))
#1 * 1 = 001
#2 * 2 = 004
#3 * 3 = 009
#4 * 4 = 016
#5 * 5 = 025

####
#formatting

print( "{0} is {1}".format("apple", "red"))
print("{0} is {1} or {2}".format("apple", "red", "green"))
#apple is red
#apple is red or green

print("{item} is {color}".format(item="apple", color="red"))
#apple is red

dic = {"item":"apple", "color":"red"}
print("{0[item]} is {0[color]}".format(dic))
#apple is red


#using local variables
item = "apple"
color = "red"

print("{0[item]} is {0[color]}".format(locals()))
#apple is red

print("{0:$=5}".format(10))
#$$$10
print("{0:$<5}".format(10))
print("{0:$>5}".format(10))
#10$$$
#$$$10
print("{0:q<5}".format(10))
print("{0:q>5}".format(10))
#10qqq
#qqq10
print("{0:#<5}".format(10))
print("{0:#>5}".format(10))
#10###
####10

print("{0:#^6}".format(10))
##10##

print("{0:x}".format(10))
print("{0:b}".format(10))
print("{0:o}".format(10))
print("{0:c}".format(65))
#a
#1010
#12
#A

print("{0:#x}, {0:#o}, {0:#b}".format(10))
#0xa, 0o12, 0b1010

print("{0:e}".format(4/3))
#1.333333e+00
print("{0:f}".format(4/3))
print("{0:%}".format(4/3))
#1.333333
#133.333333%

print("{0:.3f}".format(4/3))
#1.333

#input
#a = input('insert any key:')
#print(a)
#insert any key:sadfsaf
#sadfsaf


#file input/output
f= open('test.txt')
print(f.read())
f.close()
print( f.closed )


#binary mode and file copy

f = open('Horse2.mp3', 'wb')
f.write(open('Horse.mp3','rb').read())
f.close()




f = open('test.txt')
print( f.read() )
print( f.read() )
print( f.tell() ) #return position
print( f.seek(0) ) #move to position
print( f.read() ) #read again
f.seek(0)
print( f.readline() )
print( f.readline() )
print( f.readline() )
f.seek(0)

print( f.readlines() )

f.close()
"""
I am boy
Who are you
Hello~ world

I am boy

Who are you

Hello~ world

['I am boy\n', 'Who are you\n', 'Hello~ world\n']
"""


with open('test.txt') as f:
 print(f.readlines())
 print(f.closed) #not close

print(f.closed) #closed because if use with keyword, when escape with block file is colsed automatically

#['I am boy\n', 'Who are you\n', 'Hello~ world\n']
#False
#True

####
#pickle, save variables or class
colors = ['red', 'green', 'black']

import pickle
f = open('colors', 'wb')
pickle.dump( colors, f)
f.close()

del colors

f = open('colors', 'rb') #note! must read by binary
colors = pickle.load(f)
f.close()

print( colors )

#['red', 'green', 'black']


No comments:

Post a Comment