【python】创建,读取文件

简介:
通过指明打开的文件和模式来创建一个file类的实例。模式可以为读模式('r')、写模式('w')或追加模式('a')。
用写模式打开文件,然后使用file类的write方法来写文件,最后用close关闭这个文件。再一次打开同一个文件来读文件。如果程序中没有指定模式,读模式会作为默认的模式。在一个循环中,使用readline方法读文件的每一行。这个方法返回包括行末换行符的一个完整行。所以,当一个 空的 字符串被返回的时候,即表示文件末已经到达了,于是我们停止循环。
注意,因为从文件读到的内容已经以换行符结尾,在print语句上使用逗号来消除自动换行。
下面的例子使用了 file, open两种方式来读取文件,注意两者的不同。
#!/etc/bin/python
#!-*- coding:utf8 -*- 
# Filename using_file.py

content ='''\
this is a test file,using file class to make a new file with this text
   yangql is a boy ,like oracle ,although there is lots of things  there for me to  
   learn .I think it is a good chance to be a excellent  DBA '''

filename= 'yangql-DBA.txt'
fileobj = file(filename,'w')--以写的方式创建一个文件
fileobj.write(content)--向文件中写入内容
fileobj.close()

f = file(filename) --以file()的方式打开刚才创建的文件,默认是读。

print '-----------------make file yangql-DBA.TXT  successfully!!--------------'
print '-----------------the follow text comes from yangql-DBA.txt-------------'
print ' '
while True:
  line = f.readline()
  if len(line) ==0: 
     break
  print line,
f.close()
print ' '
print '----------------the follow text comes from open class  func------------'
fobj = open(filename,'r')
for eachline in fobj:
  print eachline,

fobj.close()                                                                                                                                                     
"using_file.py" 36L, 853C written                                                                                                                    
[yang@rac1 python]$ python  using_file.py
-----------------make file yangql-DBA.TXT  successfully!!--------------
-----------------the follow text comes from yangql-DBA.txt-------------
 
this is a test file,using file class to make a new file with this text
   yangql is a boy ,like oracle ,although there is lots of things  there for me to  
   learn .I think it is a good chance to be a excellent  DBA   
----------------the follow text comes from open class  func------------
this is a test file,using file class to make a new file with this text
   yangql is a boy ,like oracle ,although there is lots of things  there for me to  
   learn .I think it is a good chance to be a excellent  DBA 
[yang@rac1 python]$ 
相关文章
|
11天前
|
Python
Python代码扫描目录下的文件并获取路径
【5月更文挑战第12天】Python代码扫描目录下的文件并获取路径
31 1
|
4天前
|
IDE 开发工具 Python
python中SyntaxError: unexpected EOF while parsing(语法错误:解析时遇到意外的文件结束)
【5月更文挑战第14天】python中SyntaxError: unexpected EOF while parsing(语法错误:解析时遇到意外的文件结束)
24 6
|
5天前
|
数据采集 JSON 数据挖掘
2024年利用Python查询IP地址_怎么查python文件中ip地址,2024年最新15个经典面试问题及答案英语
2024年利用Python查询IP地址_怎么查python文件中ip地址,2024年最新15个经典面试问题及答案英语
|
1天前
|
Linux Python Windows
打包Python程序文件:pyinstaller实现
本文介绍基于Python语言中的pyinstaller模块,将写好的.py格式的Python代码及其所用到的所有第三方库打包,生成.exe格式的可执行文件,从而方便地在其他环境、其他电脑中直接执行这一可执行文件的方法。
|
4天前
|
安全 Python
Python 多进程日志输出到同一个文件并实现日志回滚
Python 多进程想要实现将日志输出到同一个文件中,使用同一个日志句柄,且日志需要按照日期,大小回滚。
|
4天前
|
存储 JSON 缓存
Python基础教程——文件I/O
Python基础教程——文件I/O
|
5天前
|
存储 人工智能 自然语言处理
Python编程实验五:文件的读写操作
Python编程实验五:文件的读写操作
13 0
|
5天前
|
存储 JSON JavaScript
Python文件和异常(二)
Python文件和异常(二)
15 0
|
5天前
|
存储 文件存储 Python
Python文件和异常(一)
Python文件和异常(一)
11 0
|
5天前
|
存储 文件存储 Python
Python基础 --- 实现学生管理系统(Python 文件版本)
Python基础 --- 实现学生管理系统(Python 文件版本)
21 4