Python的print方法

一般来说我们用的print 方法都是直接输出到sys.out。如print 123。

那么就有疑问了,我们用print方法输出到文本文件中,应该怎样写呢?

接下来我就讲一下具体的写法,由于python2和python3的语法有很大区别,所以就分开举例。

Python2:

s="123"
f=open("c:/1.txt","w")  #以 写 的方式打开 c:/1.txt 文件
print>>f,s
f.close()

Python3:

s="123"
f=open("c:/1.txt","w")
print (s,file=f)
f.close()

猜你喜欢

转载自blog.csdn.net/qq_38974638/article/details/88773623