源码:

poem='''\

Programming is fun

When the work is done

if you wanna make your work also fun:

use Python!

'''

f=file('poem.txt','w') # open for 'w'riting

f.write(poem) # write text to file

f.close() # close the file

f=file('poem.txt')

# if no mode is specified, 'r'ead mode is assumed by default

while True:

line=f.readline()

if len(line)==0: # Zero length indicates EOF

break

print(line)

# Notice comma to avoid automatic newline added by Python

f.close() # close the file

错误提示:

Traceback (most recent call last):

File "C:\Working\Downloads\using_file(1).py", line 11, in

f=file('poem.txt','w') # open for 'w'riting

NameError: name 'file' is not defined

解决方法:

将file()改成open(),看来python3不再支持file().

如:

poem='''\

Programming is fun

When the work is done

if you wanna make your work also fun:

use Python!

'''

f = open('c:/working/amy/poem.txt','w') # open for 'w'riting

f.write(poem) # write text to file

f.close() # close the file

f = open('c:/working/amy/poem.txt')

# if no mode is specified, 'r'ead mode is assumed by default

while True:

line=f.readline()

if len(line)==0: # Zero length indicates EOF

break

print(line)

# Notice comma to avoid automatic newline added by Python

f.close() # close the file

运行结果:

>>> ================================ RESTART ================================

>>>

Programming is fun

When the work is done

if you wanna make your work also fun:

use Python!

python3.4.4怎么用_python3.4使用文件相关推荐

  1. python3 如何读中文路径_Python3操作pdf文件之ReportLab

    一说到pdf文件,大家应该都很熟悉吧,特别是那些经常在网上搜索某本书pdf版的人.别装了,那些人当中应该就有你吧?--开玩笑的,知道你是一向只买正版纸质版的人. 买正版纸质的人?好像扯远了.好吧,言归 ...

  2. python3.8怎么打开创建_Python3.8有哪些新功能 怎么入行Python开发

    Python3.8有哪些新功能?怎么入行Python开发?近日,Python软件基金会公开了3.8版本的说明文档,向公众展示了beta版本的测试进展,以及Python3.8版本的新特性和功能改进.这些 ...

  3. python3.8新特性 逻辑表达式_Python3.8新特性

    人生天地间,忽如远行客 昨日Python3.8稳定版发布,官网发布了一篇介绍新特性的文章,在此记录一下. 新增赋值操作符 := 作用:把较大表达式中的某部分值赋给变量,因为看起来像海象的眼睛和牙齿,: ...

  4. python3语法、菜鸟教程_Python3

    Python3 floor() 函数 描述 floor(x) 返回数字的下舍整数,小于或等于 x. 语法 以下是 floor() 方法的语法: import math math.floor( x ) ...

  5. mac python3打包成窗体程序_python3代码打包成mac的程序,没办法运行,各位大神请指导一下...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 python3代码打包成mac的程序,没办法运行,打包的时候没有出错,请高手知道的说一下,我是一个小白, 这是出错的提示: Last login: Sat ...

  6. python3.7安装pip问题_python3.7安装, 解决pip is configured with locations that require TLS/SSL问题...

    1.安装相关依赖 yum install gcc libffi-devel zlib* openssl-devel # libffi-devel zlib-devel bzip2-devel open ...

  7. python3 sleep 并发_Python3并发写文件与Python对比

    这篇文章主要介绍了Python3并发写文件原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 使用python2在进行并发写的时候,发现文件会乱 ...

  8. python3.8新特性 逻辑表达式_Python3.8正式发布!新特性解析在这里

    Python3.8正式发布!新特性解析在这里 诗书塞外 Python程序员 10月14日,Python 3.8 正式版发布.这也意味着一个Python开发周期的结束,和另一个开发周期的开始.Pytho ...

  9. python3连接sql server数据库_Python3操作SQL Server数据库(实例讲解)

    1.前言 前面学完了SQL Server的基本语法,接下来学习如何在程序中使用sql,毕竟不能在程序中使用的话,实用性就不那么大了. 2.最基本的SQL查询语句 python是使用pymssql这个模 ...

最新文章

  1. Python 查看本机WiFi密码
  2. 全奖博士 | 美国康涅狄格大学计算机科学与工程系
  3. 存储时间:高清3D化,技术应用化?
  4. CentOS系统中使用yum快速安装python3
  5. 【Luogu3383】【模板】线性筛素数
  6. 数据库原理与应用(SQL Server)笔记 第五章 索引和视图
  7. 在Windows 7上安装Team Foundation Server(TFS)的代理服务器(Agent)
  8. [图]运动鞋用“囧”字做图案卖断货
  9. 國慶和中秋的學習成果
  10. Tomcat 指定jdk
  11. 一段最短的代码,用上js所有关键字
  12. 本周题解(9.12)
  13. 软件测试工具Winrunner使用的技巧
  14. 蓝牙 - BQB认证测试
  15. 凭什么软件测试入门就有一万+工资,为什么?我不相信。
  16. 使用threejs开发微信小游戏全过程
  17. 如何解决“被调用的对象已与其客户端断开连接“
  18. 软件测试查漏补缺(一)——驱动开发、系统测试、测试工具
  19. “拆股”买投资房正在成为现实,不用攒钱也能买上房
  20. eplan好看的电缆图表_eplan中怎样才能把整个项目的端子图表或者电缆图表生成到我指定的位置...

热门文章

  1. easyui中combobox 验证输入的值必须为选项框中的数据
  2. RabbitMQ自学之路(九)——RabbitMQ实现延时队列的两种方式
  3. python mysql curros.executemany 批量添加
  4. PyCharm设置字体大小(亲测)
  5. ELK+Kafka 企业日志收集平台(一)
  6. linux中设置环境变量PATH的方法(转)
  7. c语言的查询功能,求C语言实现查询功能(如果选择3,如何实现查询)
  8. k8s angular mysql_Angular 实践:如何优雅地发起和处理请求
  9. 动画,视频处理的计算机系统,音视频与动画处理.ppt
  10. 阿里云debian mysql_记录下在阿里云linux上安装与配置Mysql