python 如何将代码中的输入保存到txt里边

一、在 print 前建立一个txt

python3:

f = open('print.txt', 'w')
print('this is a txt', file = f)
f.close()

python2:

f = open('print.txt', 'w')
print>>f, 'this is a txt'
f.close()

二、在控制台输入

python print_txt.py > print.txt
发布了152 篇原创文章 · 获赞 716 · 访问量 69万+

猜你喜欢

转载自blog.csdn.net/jiaoyangwm/article/details/104728627