我只需要将python [V:2.6]脚本运行为./test.py。print(a)的输出是“ hello world”,应保存到“ file.txt”中,如何在python脚本中执行此操作?我知道,
./test.py > file.txt
test.py
#!/usr/bin/python
a = 'hello world'
print(a)
f = open('/root/file.txt', 'w')
#!/usr/bin/python
a = 'hello world'
f = open('/root/file.txt', 'w')
print >>f, a
现在,在运行./test.py时,“ hello world”将被打印在file.txt中。这很简单,但可能对python初学者很有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句