比如.py和data文件夹都在project目录下
temp.txt已经创建好在data文件夹下
则
txtpath='./data/temp.txt'
if os.path.exists(txtpath):
print("ok exists" )
else:
print("wrong cant find")
fobj=open(txtpath,'w')
fobj.write('whattsup_man!\n')
fobj.write("yeap")
fobj.close()
读取txt
一次性所有行都读入
txtpath='./data/temp.txt'
f = open(txtpath)
xx=f.readlines()
print(xx)
则会输出
[‘hello boy!\n’, ‘yeap’]