【python】输出到文件, f.write与print

57 篇文章 13 订阅

将控制台print的信息输出到文件,py2.x可以利用流输入符>>,py3.x可以使用file参数

1.输出到文件 I/O

将信息输出到文件最直接的方法是使用文件I/O:

f = open('log.txt','w')
for i in range(100):
    f.write(str(i)+'\n')
f.close()
# 生成log.txt文件
>>>
1
2
3
...
100

2.输出到文件 print 函数

print函数除了打印到控制台,同时还提供了输出到文件的功能,其默认输出文件是sys.stdout,意味着控制台输出。如果感兴趣可以看更详细的说明.

##########################
# ---------py2.x-------- #
f = open('log.txt','w')
for i in range(100):
    # print >> f, str(i)+'\n'
    print >> f, str(i)    #print函数加了\n,不需要再加了
f.close()

>>>
1
2
3
...
100

##########################
# ---------py3.x-------- #
f = open('log.txt','w')
for i in range(100):
    print(str(i), file=f)
f.close()

>>>
1
2
3
...
100

3.print doc

最后给出print函数的参考文档,除了需要打印的值value外,还有sep分割符号,en d结束符,flush强制流输出,file目标文件等四个参数。

print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

"""
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
"""
ref:
https://www.python.org/dev/peps/pep-3105/#id9
https://docs.python.org/2/reference/simple_stmts.html#print
https://zhuanlan.zhihu.com/p/86859011
https://blog.csdn.net/u013783249/article/details/80669634
https://blog.csdn.net/u012145971/article/details/81207303
https://blog.csdn.net/liweiblog/article/details/53198468
https://blog.csdn.net/yageeart/article/details/38386121

https://images.pexels.com/photos/1742926/pexels-photo-1742926.jpeg
pic from pexels.com

Python中,我们可以使用print语句将输出内容打印到文本文件中。通过重定向输出流,我们可以将print语句的输出定向到指定的文件中。引用和引用提到了这个方法。 具体实现步骤如下: 1. 首先,我们需要打开一个文件,以便将print语句的输出写入该文件。可以使用内置的open函数来打开文件,并指定文件名和打开模式(如写入模式'w')。 2. 接下来,可以使用print语句并将输出内容作为参数传递给它。在print语句中,使用文件对象的write方法将输出内容写入到文件中。在每个print语句后面,我们可以使用文件对象的write方法写入换行符'\n',以确保每条输出单独一行。 3. 最后,不要忘记关闭文件,以确保输出内容被正确保存。 以下是一个示例代码,演示了将print语句输出到文本文件的过程: ``` # 打开文件,以写入模式打开 file = open('output.txt', 'w') # 重定向输出文件 import sys sys.stdout = file # 使用print语句输出内容 print("Hello, World!") print("This is a test.") # 关闭文件 file.close() ``` 在上面的代码中,我们首先使用open函数打开名为output.txt的文件,并以写入模式打开它。然后,通过将sys.stdout重定向到打开的文件,我们将print语句的输出定向到该文件。在最后,我们关闭了文件。 请注意,通过重定向输出流,print语句将不再在屏幕上显示输出内容,而是写入到指定的文件中。这意味着,在使用完毕后,需要将sys.stdout重新设置回原始值,以确保后续的print语句正常显示在屏幕上。引用提到了原始的输出流应该保持不变。 希望这个解答对你有帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python: print输出到文本文件中;赋值语句的执行流程](https://blog.csdn.net/qq_41035283/article/details/127831939)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值