1.文件的读写
text='this is my first test.\nthis is next line.\nthis is last line'
#\n代表在文本中换行
my_file=open('my file.txt','w')
#open(a,b)a代表创建或者打开文件名字,b代表打开形式,w代表写文件
#r代表读文件,a代表往里面追加内容
#my_file.write(text)
#打开文件后一定要记得关上文件
my_file.close()
text为往文件里面插入的内容
2.往文件里面插入内容
#往文件里面插入内容
append_text='\nthis is append file'
my_file = open('my file.txt','a')
my_file.write(append_text)
my_file.close()
3.读文件
#读文件
file=open('my file.txt','r')
#并不是把内容存在file里面,而是把文件存在file里面
content=file.read()
print(content)
read函数,open函数,write函数,无论对文件做什么操作,都先需要对文件open
4.input函数
a=input('please give input')
#input返回的是一个字符串,即返回的是'1'的形式
if a==str(1):
#判断条件需为字符串
print('a is one')
elif a=='2':
print('a is two')
else:
print('a is other')