print 输出到文件
content = """We have seen thee, queen of cheese,
Lying quietly at your ease,
Gently fanned by evening breeze,
Thy fair form no flies dare seize.
All gaily dressed soon you'll go
To the great Provincial show,
To be admired by many a beau
In the city of Toronto.
Cows numerous as a swarm of bees,
Or as the leaves upon the trees,
It did require to make thee please,
And stand unrivalled, queen of cheese.
May you not receive a scar as
We have heard that Mr. Harris
Intends to send you off as far as
The great world's show at Paris.
Of the youth beware of these,
For some of them might rudely squeeze
And bite your cheek, then songs or glees
We could not sing, oh! queen of cheese.
We'rt thou suspended from balloon,
You'd cast a shade even at noon,
Folks would think it was the moon
About to fall and crush them soon.
"""
1.python3版本
f = open('poem.txt','w')
print(content,file=f)
f.close()
2.python2版本
f = open('poem.txt','w')
print >> f,content #使用输出重定向 ,注意只是将内容写入了文件的缓冲区中,还没有写入文件
f.close() #关闭文件描述符,将文件缓冲区中的内容从内存写入磁盘文件
print 输出到文件的更多相关文章
- python内置函数print输出到文件,实现日志记录的功能
# bulid time 2018-6-22 import os import time def log(*args, **kwargs): # *kargs 为了通用 可不传 rule = &quo ...
- python print输出到文件
要将程序的输出送到一个文件中,需要在 print 语句后面使用 >> 指定一个文件,如下所示: principal = # 初始金额 rate = 0.05 # 利率 numyears = ...
- Python Linux 命令行执行脚本输出重定向print到日志文件
reference: https://unix.stackexchange.com/questions/182537/write-python-stdout-to-file-immediately ...
- (Python )格式化输出、文件操作、json
本节学习Python的格式化输出,文件操作以及json的简单用法 1.格式化输出 将非字符串类型转换成字符串,可以使用函数:str() 或者repr() ,(这两个函数的区别目前我还没搞懂,求解答) ...
- linux bash脚本把A和B文件中有相同ID的B文件的内容输出到文件C
bash脚本把A和B文件中有相同ID的B文件的内容输出到文件C. Aid文件:ID001.1ID032.1ID090.10 Bfilt文件:XX XX XXX ID001.1 XXX999999999 ...
- 用otl写的oracle取数工具,执行传入在查询语句,把结果输出到文件
项目中经常需要用到此类型的工具 #pragma warning (disable:4786) #include <iostream> #include <map> #inclu ...
- [Head First Python]4.读取文件datafile.txt, 去除两边空格, 存储到列表,从列表格式化(nester.py)后输出到文件man.out,other.out
datafile.txt #文件 Man: this is the right room for an argument. Other Man: I've told you once. Man: N ...
- ASP.NET Core 2.1 : 十二.内置日志、使用Nlog将日志输出到文件
应用离不开日志,虽然现在使用VS有强大的调试功能,开发过程中不复杂的情况懒得输出日志了(想起print和echo的有木有),但在一些复杂的过程中以及应用日常运行中的日志还是非常有用. ASP.NET ...
- Java的实验程序之输出单个文件中的前 N 个最常出现的英语单词
日期:2018.10.11 星期四 博客期:016 题目:输出单个文件中的前 N 个最常出现的英语单词,并输出到文本文件中 在程序运行之前,我试着先写了字符的字母的总结,加载代码如下: //如下是第一 ...
随机推荐
- Arrays 类的 binarySearch() 数组查询方法详解
Arrays类的binarySearch()方法,可以使用二分搜索法来搜索指定的数组,以获得指定对象.该方法返回要搜索元素的索引值.binarySearch()方法提供多种重载形式,用于满足各种类型数 ...
- P2157 [SDOI2009]学校食堂 (dp+状态压缩)
题目链接:传送门 题目: 题目描述 小F 的学校在城市的一个偏僻角落,所有学生都只好在学校吃饭.学校有一个食堂,虽然简陋,但食堂大厨总能做出让同学们满意的菜肴.当然,不同的人口味也不一定相同,但每个人 ...
- day 014 python 内置函数
1. lamda匿名函数2. sorted()3. filter()4. map()5. 递归函数 一 匿名函数(lambda) 函数名= lambda 参数: 返回值 简单算法 a+b 常规算 ...
- HDU 2048:神、上帝以及老天爷(错排公式,递推)
神.上帝以及老天爷 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- 动手动脑-Java的继承与多态
一. class Grandparent { public Grandparent() { System.out.println("GrandParent Created."); ...
- acm 2032
////////////////////////////////////////////////////////////////////////////////#include<iostream ...
- String中concat方法小记
介绍String中的concat方法使用: 日常开发中,经常对字符串进行处理,之前碰到多个字符串拼接,要么使用stringBuilder,要么使用StringBuffer,再或者是直接多个String ...
- Javascript中的 “&” 和 “|” 详解
转自:https://www.jb51.net/article/104394.htm 一.前言: 在文章开始之前,先出几个题目给大家看看: var num1 = 1 & 0; console. ...
- 【UOJ#21】【UR#1】缩进优化
我好弱啊,什么题都做不出来QAQ 原题: 小O是一个热爱短代码的选手.在缩代码方面,他是一位身经百战的老手.世界各地的OJ上,很多题的最短解答排行榜都有他的身影.这令他感到十分愉悦. 最近,他突然发现 ...
- heptio scanner kubernetes 集群诊断工具部署说明
heptio scanner 是一款k8s 集群状态的诊断工具,还是很方便的,但是有一点就是需要使用google 的镜像 参考地址 https://scanner.heptio.com/ 部署 kub ...