设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 40469|回复: 3
打印 上一主题 下一主题

【Python 第32课】 写文件

[复制链接]

0

主题

2

好友

72

积分

注册会员

Rank: 2

楼主
发表于 2018-4-9 18:52:23 |显示全部楼层
  1. #homework.No.1
  2. #司南 2018.04.09
  3. #从文件中读取内容
  4. f_read=open('while.py').read()
  5. f_save=open('save.py','w')
  6. f_save.write(f_read)
  7. f_save.close()
  8. data=open('save.py').read()
  9. print(data)
复制代码
回复

使用道具 举报

0

主题

2

好友

72

积分

注册会员

Rank: 2

沙发
发表于 2018-4-9 19:06:19 |显示全部楼层
Crossin先生,我实在是不知道怎么办了,为啥这个代码一执行到,输入done,python就死机呢
  1. #homework.No.1
  2. #司南 2018.04.09
  3. #从控制台输入内容,保存至文件
  4. string=input('You can add any words to the document,Please begin,and end with "done"')
  5. while string!='done':
  6.     f_desk=open('desk.txt','a+')
  7.     f_desk.write(string+'\n')
  8.     f_desk.close()
  9.     string=input('You can add any words to the document,Please begin,and end with "done"')
  10. data=open('desk.txt').read()
  11. print(data)
  12. data.close()
复制代码
回复

使用道具 举报

0

主题

2

好友

72

积分

注册会员

Rank: 2

板凳
发表于 2018-4-10 15:12:36 |显示全部楼层
crossin先生 发表于 2018-4-9 23:16
因为死循环了啊
程序里多增加一些print输出你就看出来了
  1. #homework.No.2
  2. #司南 2018.04.09
  3. #从控制台输入内容,保存至文件
  4. string=input('You can add any words to the document,Please begin,and end with "done"')
  5. while string!="done":
  6.     print(string)
  7.     f_desk=open('desk.txt','a+')
  8.     f_desk.write(string+'\n')
  9.     f_desk.close()
  10.     string=input('You can add any words to the document,Please begin,or end with "done"')
  11.     print(string)
  12. f=open('desk.txt')
  13. data1=f.read()
  14. print(data1)
  15. f.close()
复制代码
Crossin先生,这个加了print试了一下,每次print也是对的,但是一点输入done,python就是转圈圈,未响应,您知道为啥麽
回复

使用道具 举报

0

主题

2

好友

72

积分

注册会员

Rank: 2

地板
发表于 2018-4-10 15:54:44 |显示全部楼层
crossin先生 发表于 2018-4-10 15:52
我这边执行你的代码没有问题

换个文件或者换个代码执行环境试试吧

多谢先生,已解决,更换文件名字就可以了
回复

使用道具 举报

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

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

GMT+8, 2024-5-20 05:31 , Processed in 0.026260 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部