注册 登录
编程论坛 Python论坛

Python 怎么打开文件

q18024155574 发布于 2014-11-27 16:47, 2759 次点击
>>> context = "'hello worldhello china'"
>>> f = open('hello.txt','w')
>>> f.write(context)
>>> f.close()
为什么我打开不了,新人求不喷
9 回复
#2
静夜思2014-11-27 16:54
确定文件是否存在,另外,错误提示是什么?
#3
q180241555742014-11-27 17:00
回复 2 楼 静夜思
>>> context = "'hello world hello china'"
>>> f= = file('hello.txt','w')
SyntaxError: invalid syntax
>>> f = file('hello.txt','w')
>>> f.write(context)
>>> f.close
<built-in method close of file object at 0x0000000002846420>
>>>
没有错误提示吧,可是我不知道怎么用python对文本内容进行编写啊,难道不能用phthon(GIU)进行编写,要用脚本?
#4
静夜思2014-11-27 17:01
f.close后面要带括号
#5
q180241555742014-11-27 17:03
回复 4 楼 静夜思
Type "copyright", "credits" or "license()" for more information.
>>> context = "'hello world hello china'"
>>> f= = file('hello.txt','w')
SyntaxError: invalid syntax
>>> f = file('hello.txt','w')
>>> f.write(context)
>>> f.close
<built-in method close of file object at 0x0000000002846420>
>>> f.close()
>>>
那然后呢
#6
q180241555742014-11-27 17:05
回复 4 楼 静夜思
我确实是没有创建文档的,不是没创建文档的话会自动生成一个吗
还有假如是自动生成文档,那文档自动保存在哪呢
#7
静夜思2014-11-27 17:05
没出错,证明程序正常,hello world hello china已经被成功写进hello.txt
#8
q180241555742014-11-27 17:08
回复 7 楼 静夜思
可是我没找到hello.txt
在哪看的啊
#9
静夜思2014-11-27 17:11
退出python的交互式环境 exit()
然后所在的目录下应该就能找到hello.txt
#10
q180241555742014-11-27 17:23
回复 9 楼 静夜思
找到了,万谢
1