首页 > 编程语言 >Python - Redirecting output of print to a file

Python - Redirecting output of print to a file

时间:2024-07-30 15:18:54浏览次数:10  
标签:function Python will file print output Redirecting

The print function can also be used to write to a file. The output of print, that is by default, sent to the screen can be redirected to an open file. For this, you have to supply the file object as an argument for the named parameter file. Here is an example:

x = 3567

with open('data2.txt', 'w') as f:

print('Ultimate Python', x, file=f)

The output produced by print will be written to data2.txt file. The value of variable x will be stored as sequence of 4 characters not as an integer, since we are working in text mode. When we write to a file using the print function, the newline will automatically be written since that is the default behaviour of print. If we want to suppress it, we can supply an argument for the named parameter end. This redirection of output to a file using the print function will work only in text mode.

 

标签:function,Python,will,file,print,output,Redirecting
From: https://www.cnblogs.com/zhangzhihui/p/18332489

相关文章

  • Python:添加到相对于当前运行脚本的 sys.path 的最佳方法
    我有一个充满脚本的目录(比如说project/bin)。我还有一个位于project/lib的库,并希望脚本自动加载它。这是我通常在每个脚本的顶部使用的:#!/usr/bin/pythonfromos.pathimportdirname,realpath,sep,pardirimportsyssys.path.append(dirname(realpath(_......
  • python身份证号码+姓名一致性核验、身份证号码真伪查询API集成
    身份证号码+姓名核验的方式,顾名思义是身份证二要素核验,一般情况下,身份证真伪查询需要上公安户籍系统查询,但此种方式仅适合个人查询,企业要想随时随地实现身份证实名认证的功能,便需要集成身份证实名认证接口功能。翔云人工智能开放平台提供身份证号实名认证接口,实时联网,上传身份证......
  • 如何将数字分配给返回的 python 数据列表,我可以调用这些数据来打印
    这里完全是菜鸟。我在网上搜索过,找不到我想要做的事情的答案。我的代码在这里:importbs4asbsimporturllib.requestsauce=urllib.request.urlopen('https://www.amazon.com/gp/rss/bestsellers/kitchen/289851/ref=zg_bs_289851_rsslink').read()soup=bs.Beautiful......
  • python API增值税发票四要素核验、数电票查验、医疗票查验
    长期以来,对发票进行高效的管理一直困扰着众多企业财务,手动录入效率慢、出错率高、纸质发票易丢失等。今天,翔云为广大企业提供了发票查验接口与财政票据查验接口服务,可针对增值税发票管理系统开具发票,医疗票据、非税收入等财政类票据进行真伪查验。翔云发票识别接口,使得企业财务无......
  • 如何使用 python 在 influxdb 中创建组织和存储桶
    如何使用python在influxdb中创建组织和存储桶?我有一个python脚本,用于在influxdb中创建组织和存储桶,但它无法工作并返回未经授权的响应任何人可以使用influxdbapi帮助我解决这个问题吗?HTTPresponsebody:{"code":"unauthorized","message":"write:org......
  • Python - File opening modes and buffering
    'r'-readmode(default)'w'-writemode'a'-appendmode'x'-exclusivecreationWeknowthatthemode'r'opensanexistingfileforreadingonly;thefileshouldalreadyexist.Ifyouopenafilein......
  • 如何使用 Python 对图像中的掩模部分进行聚类?
    我需要以这样的方式拆分蒙版:如果蒙版内存在不一致,则会将其分开。例如,如果我在一只猫上画一个面具,我希望宽的部分(身体)是一个面具,窄的部分(尾巴)是另一个面具。目前,我有一个连续的面具,其中包括两者猫的身体和尾巴。我想将其分成两个不同的面具。如何使用Python实现此目的?原......
  • 如何在 python 中为具有不同类型作为值的字典添加类型声明
    我有一个字典如下my_dict={"key_1":"value_1","key_2":{"key_1":True,"key_2":1200}"key_3":True,}并且在我的类中@dataclassclassTestClass:my_dict:typing......
  • Python TypedDict:继承另一个TypedDict时的函数语法
    给定这种类型:classTPerson(TypedDict):name:straddress:str我想要另一个TypedDict继承前一个,例如:classTDealer(TPerson):db-id:intpolice_record:strarrested_now:boolclassTConsumer(TPerson):db-id:intpreferred_product:......
  • 如何让 python 类型在需要父类时将子类识别为有效类型?
    这是我需要做的一个最小示例:fromtypingimportCallable,AnyclassData:passclassSpecificData(Data):passclassEvent:passclassSpecificEvent(Event):passdefdetect_specific_event(data:SpecificData,other_info:str)->Specif......