我们可以通过print 的重定向功能来向一个文件中输出信息,具体如下:
最核心的代码为:
print >> logfile,’This is a test message’
完整代码如下:
1 2 3 4 5 6 7 8 9 10 |
>>> import sys >>> logfile=open('/tmp/mylog.txt','a') >>> print >> logfile,'This is a test message' >>> logfile.close() >>> exit Use exit() or Ctrl-D (i.e. EOF) to exit >>> exit() [zhiming]$ cat /tmp/mylog.txt This is a test message []$ |
另外,我们一般打印的print是一个一行,如果我们希望print的想在一行话
1 2 3 4 5 6 |
>>> a = ['eamil','book','tel','zhi'] >>> for item in a: ... print item, ... eamil book tel zhi >>> |
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021