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 输出到文件的更多相关文章

  1. python内置函数print输出到文件,实现日志记录的功能

    # bulid time 2018-6-22 import os import time def log(*args, **kwargs): # *kargs 为了通用 可不传 rule = &quo ...

  2. python print输出到文件

    要将程序的输出送到一个文件中,需要在 print 语句后面使用 >> 指定一个文件,如下所示: principal = # 初始金额 rate = 0.05 # 利率 numyears = ...

  3. Python Linux 命令行执行脚本输出重定向print到日志文件

    reference: https://unix.stackexchange.com/questions/182537/write-python-stdout-to-file-immediately   ...

  4. (Python )格式化输出、文件操作、json

    本节学习Python的格式化输出,文件操作以及json的简单用法 1.格式化输出 将非字符串类型转换成字符串,可以使用函数:str() 或者repr() ,(这两个函数的区别目前我还没搞懂,求解答) ...

  5. 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 ...

  6. 用otl写的oracle取数工具,执行传入在查询语句,把结果输出到文件

    项目中经常需要用到此类型的工具 #pragma warning (disable:4786) #include <iostream> #include <map> #inclu ...

  7. [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 ...

  8. ASP.NET Core 2.1 : 十二.内置日志、使用Nlog将日志输出到文件

    应用离不开日志,虽然现在使用VS有强大的调试功能,开发过程中不复杂的情况懒得输出日志了(想起print和echo的有木有),但在一些复杂的过程中以及应用日常运行中的日志还是非常有用. ASP.NET ...

  9. Java的实验程序之输出单个文件中的前 N 个最常出现的英语单词

    日期:2018.10.11 星期四 博客期:016 题目:输出单个文件中的前 N 个最常出现的英语单词,并输出到文本文件中 在程序运行之前,我试着先写了字符的字母的总结,加载代码如下: //如下是第一 ...

随机推荐

  1. xdoj 1067组合数学+动态规划 (一个题断断续续想了半年 233)

    题目分析 : (8 4) 可以由(7 4),(6,4),( 4,4) 基础上转化 意味着一个新加入的元素可以按照它加入的方式分类,从而实现动态规划 核心:加入方式 新加入的元素构成转换环的元素个数(n ...

  2. linux 执行脚本

    crontab -e 直接编辑定时脚本 保存后,在/var/spool/cron/下自动生成一个以用户名命名的脚本文件.

  3. 初始if..else 条件语句

    if..else条件语句 如果我们希望有效的响应用户的输入,代码就需要具有判断能力.能够让程序进行判断的结构成为条件,条件判断语句返回的是布尔值真或假,真就执行一条线路,假就执行另外一条线路 if 条 ...

  4. 生产环境部署MongoDB副本集(带keyfile安全认证以及用户权限)

    本文同步于个人Github博客:https://github.com/johnnian/Blog/issues/8,欢迎留言. 安装软件包:mongodb-linux-x86_64-3.4.1.tgz ...

  5. zabbix监控mysql最简单的方法

    该实验基于我的上一篇文章监控第一台主机的基础上 首先,因为水平有限,我选择直接关闭了防火墙和SELinux. 环境: 两台centos7,服务器端IP是192.168.200.128(以下简称主机), ...

  6. SWF 文件不能本地访问 只有仅限于文件系统的 SWF 文件

    http://blog.163.com/vituk93@126/blog/static/1709580342012512112757505/ SWF 文件不能被本地访问 不能访问本地 只有仅限于文件系 ...

  7. node nightmare 网页自动化测试 sample

    安装nightmare 编写1.js 运行 node 1.js var Nightmare = require('nightmare');var nightmare = Nightmare({ sho ...

  8. asterisk todo

    1, 如何让用户打一个密码才能继续外拨? 可以利用__9xxx那种做法 2,如何和onsip连起来? 3, 如何记录所有拨打记录?

  9. output.filename 与 output.chunkFilename 的区别

    // webpack.config.js module.exports = { entry: './src/index.js', output: { filename: '[name].bundle. ...

  10. webpack 的 入口(Entry)、输出(Output)

    入口(Entry) 入口定义了我们的应用代码开始执行的那个文件,webpack从这个文件开始打包.你能定义一个入口点(常见于单页应用 - Single-Page Application), 或者多个入 ...