import PrintHelper
import os
PrintHelper.PrintTitle('Files')
PrintHelper.PrintSubtitle('Prepares')
PrintHelper.PrintCode('os.chdir(\"..\\2.Data\")')
PrintHelper.PrintCode('print(os.getcwd())')
PrintHelper.PrintCode('filePath="a.txt"')
PrintHelper.PrintCode('print(filePath)')
os.chdir("..\\..\\2.Data")
print(os.getcwd())
filePath="a.txt"
print(filePath)
PrintHelper.PrintSubtitle('Reading Text Files')
PrintHelper.PrintHint('open(@path,@mode)')
PrintHelper.PrintHint('read(@path,@mode)')
print('open(@path,@mode)')
print('@mode有以下模式')
print('r或省略:只读')
print('\"w\":只写,总是覆盖创建文件')
print('\"w+\":读写,总是覆盖创建文件')
print('\"a\":append,文件不存在时创建')
print('@path默认使用当前的路径')
PrintHelper.PrintCode('with open(filePath,"a") as fp:')
PrintHelper.PrintCode('    fp.close()')
PrintHelper.PrintCode('with open(filePath) as fp:')
PrintHelper.PrintCode('    buffer=fp.read()')
PrintHelper.PrintCode('print(buffer)')
with open(filePath,"a") as fp:
    fp.close()
with open(filePath) as fp:
    buffer=fp.read()
print(buffer)
PrintHelper.PrintHint('@stream.readline()')
PrintHelper.PrintCode('with open(filePath,\"r\") as fp:')
PrintHelper.PrintCode('    while True:')
PrintHelper.PrintCode('        buffer=fp.readline()')
PrintHelper.PrintCode('        if buffer==\"\":')
PrintHelper.PrintCode('            break')
PrintHelper.PrintCode('        print(buffer)')
PrintHelper.PrintCode('print(buffer)')
with open(filePath,"r") as fp:
    while True:
        buffer=fp.readline()
        if buffer=="":
            break
        print(buffer)
print(buffer)
PrintHelper.PrintHint('@stream.readlines()')
PrintHelper.PrintCode('fp=open(filePath)')
PrintHelper.PrintCode('buffer=fp.readlines()')
PrintHelper.PrintCode('fp.close()')
PrintHelper.PrintCode('for line in buffer:')
PrintHelper.PrintCode('    print(line)')
fp=open(filePath)
buffer=fp.readlines()
fp.close()
for line in buffer:
    print(line)
PrintHelper.PrintHint('@stream.close()')
print('已在前面做演示')
PrintHelper.PrintSubtitle('Writing Text Files')
PrintHelper.PrintHint('@stream.write()')
PrintHelper.PrintCode('with open(filePath,\"w+\") as fp:')
PrintHelper.PrintCode('    fp.write(\"123456789\")')
with open(filePath,"w") as fp:
    fp.write("123456789")
PrintHelper.PrintHint('@stream.writelines()')
PrintHelper.PrintCode('with open(filePath,\"w+\") as fp:')
PrintHelper.PrintCode('    fp.writelines([\"abc\",123,\"0919\"])')
with open(filePath,"w") as fp:
    fp.writelines(["abc","123","0919"])

PrintHelper.PrintSubtitle('Casting,类型转换')
PrintHelper.PrintCode('bs=\"1\".encode(\"utf-8\")')
bs="1".encode("utf-8")
PrintHelper.PrintCode('print(bs)')
print(bs)
PrintHelper.PrintCode('s=bs.decode(\"utf-8\")')
s=bs.decode("utf-8")
PrintHelper.PrintCode('print(s)')
print(s)
PrintHelper.PrintCode('print(int(s))')
print(int(s))
PrintHelper.PrintCode('print(float(s))')
print(float(s))
PrintHelper.PrintCode('print(complex(1))')
print(complex(1))
PrintHelper.PrintCode('print(complex(1,2))')
print(complex(1,2))
PrintHelper.PrintCode('print(bytes(s,\"utf-8\"))')
print(bytes(s,"utf-8"))
PrintHelper.PrintCode('print(bytes(b\'1\'))')
print(bytes(b'1'))
