【python】创建,读取文件

简介: 通过指明打开的文件和模式来创建一个file类的实例。模式可以为读模式('r')、写模式('w')或追加模式('a')。用写模式打开文件,然后使用file类的write方法来写文件,最后用close关闭这个文件。
通过指明打开的文件和模式来创建一个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]$ 
目录
相关文章
|
1月前
|
存储 JSON JavaScript
Python中读写(解析)JSON文件的深入探究
Python中读写(解析)JSON文件的深入探究
24 0
|
1月前
|
XML 安全 API
Python读写XML文件:深入解析与技术实现
Python读写XML文件:深入解析与技术实现
40 0
|
2月前
|
存储 数据挖掘 数据处理
Python与Excel的交互:读写Excel文件和处理数据
Python与Excel的交互:读写Excel文件和处理数据
|
8月前
|
Python
python读写execle文件数据
python读写execle文件数据
|
4月前
|
XML 存储 JavaScript
【python】DOM模块读写XML文件
【python】DOM模块读写XML文件
22 0
|
10月前
|
Python
Python应用 | 读写docx文件 (值得收藏的技能)
Python应用 | 读写docx文件 (值得收藏的技能)
101 0
|
10月前
|
算法 数据处理 Python
R和Python平台下操作读写稀疏矩阵(matrix.mtx.gz格式文件)的基本方法
将大型矩阵保存为稀疏矩阵格式特别有助于减少存储空间和提高数据处理的效率,因此本文将分享在R和Python平台下操作读写稀疏矩阵的基本方法。
699 0
|
XML 数据格式 Python
Python通过docx模块读写微软docx文件
Python通过docx模块读写微软docx文件
143 0
|
Python
Python编程:读写excel文件
Python编程:读写excel文件
112 0
|
Python
python自动化之使用xlwings读写excel文件
xlwings的意思是给Excel插上翅膀,官网解释为Make Excel Fly。 xlwings是一个可以实现从Excel调用Python,也可在python中调用Excel的库。开源免费,一直在更新。特点:
497 0