我只需将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')
答案 0 :(得分:0)
更好的解决方案: Python print语句得到了名为 file
的关键字参数print('spam', file=<YOUR FILE>)
答案 1 :(得分:-2)
#!/usr/bin/python
a = 'hello world'
f = open('/root/file.txt', 'w')
print >>f, a
现在“hello world”将在运行./test.py时打印在file.txt中 这很简单,但可能对python初学者有用。