文件
file = open('./txt.txt')
text = file.read()
print(text)
file.close()
file = open('./txt.txt')
text = file.read()
print(text)
print(len(text))
print('-' * 50)
text2 = file.read()
print(text2)
print(len(text2))
file.close()
file = open('./text.txt', 'a')
file.write('hello world!')
file.close()
file = open('./txt.txt')
while True:
text = file.readline()
if not text:
break
print(text)
file.close()
file_read = open('./txt.txt')
file_write = open('./txt[附件].txt', 'w')
text = file_read.read()
file_write.write(text)
file_read.close()
file_write.close()
""" 复制大文件 """
file_read = open('./txt.txt')
file_write = open('./txt[附件].txt', 'w')
while True:
text = file_read.readline()
if not text:
break
file_write.write(text)
file_read.close()
file_write.close()
input_str = input('输入算术题:')
print(eval(input_str))
import os
print(os.getcwd())
print(os.path.abspath('.'))
print(os.path.abspath('txt.txt'))
print(os.path.abspath('..'))
print(os.path.abspath(os.curdir))
print(os.listdir())
from contextlib import contextmanager
@contextmanager
def my_open(path, mode):
f = open(path, mode)
yield f
f.close()
with my_open('out.txt', 'w') as f:
f.write('hello , the simplest context manager')
with open('out.txt', 'w') as f:
f.write('hello , the simplest context manager')
with open('./1.txt', 'wb') as f:
f.write('hello flask'.encode('utf-8'))
class Foo(object):
def __enter__(self):
""" 进入with语句的时候被with调用 """
print('enter called')
def __exit__(self, exc_type, exc_val, exc_tb):
""" 离开with语句的时候被with调用 """
print('exit called')
print('exc_type: %s' % exc_type)
print('exc_val: %s' % exc_val)
print('exc_tb: %s' % exc_tb)
with Foo() as foo:
print('hello python')
a = 1 / 0
print('hello end')