设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

【Python 第32课】 写文件

[复制链接]

1

主题

0

好友

80

积分

注册会员

Rank: 2

楼主
发表于 2017-8-25 07:53:36 |显示全部楼层
第一题:
  1. f = file('data.txt')
  2. data = f.read()
  3. print data
  4. f.close()

  5. #f1 = file('target.txt')  python默认是以只读模式打开文件。如果想要写入内容,在打开文件的时候需要指定打开模式为写入
  6. f1 = file('target.txt','w')
  7. f1.write(data)
  8. #data1 = f1.read()
  9. #print data1
  10. f1.close()
  11. #print f1.read()

  12. f2 = file('target.txt')
  13. data1 = f2.read()
  14. print data1
复制代码
第二题:
  1. s = raw_input()

  2. f = file('output.txt','w')

  3. f.write(s)

  4. f.close()

  5. f1 = file('output.txt')

  6. data = f1.read()

  7. print data
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-8 03:07 , Processed in 0.033488 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部