【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]$ 
相关文章
|
21天前
|
Python
python文件读写操作的三大基本步骤
python文件读写操作的三大基本步骤
36 0
|
4天前
|
Shell Python
Python Stock guess_indicators_daily_job.py文件的调整
Python Stock guess_indicators_daily_job.py文件的调整
12 1
|
4天前
|
XML 前端开发 数据格式
BeautifulSoup 是一个 Python 库,用于从 HTML 和 XML 文件中提取数据
【5月更文挑战第10天】BeautifulSoup 是 Python 的一个库,用于解析 HTML 和 XML 文件,即使在格式不规范的情况下也能有效工作。通过创建 BeautifulSoup 对象并使用方法如 find_all 和 get,可以方便地提取和查找文档中的信息。以下是一段示例代码,展示如何安装库、解析 HTML 数据以及打印段落、链接和特定类名的元素。BeautifulSoup 还支持更复杂的查询和文档修改功能。
13 1
|
2天前
|
Python
在Python中,利用`os模块`的`path.exists()`函数可判断文件是否存
【5月更文挑战第12天】在Python中,利用`os模块`的`path.exists()`函数可判断文件是否存在,该函数对路径进行检查,存在则返回True,不存在则返回False。示例代码展示了如何检查'example.txt'文件是否存在并相应打印消息。此外,`os.path.isfile()`用于确认路径是否为文件,仅当是文件时返回True,否则返回False,同样配以示例说明其用法。
11 2
|
5天前
|
数据采集 NoSQL 中间件
python-scrapy框架(四)settings.py文件的用法详解实例
python-scrapy框架(四)settings.py文件的用法详解实例
9 0
|
5天前
|
存储 数据采集 数据库
python-scrapy框架(三)Pipeline文件的用法讲解
python-scrapy框架(三)Pipeline文件的用法讲解
7 0
|
7天前
|
缓存 数据处理 Python
python读取文件到缓存
python读取文件到缓存
12 1
|
8天前
|
存储 数据挖掘 Python
Python技术分享:实现选择文件或目录路径的方法
Python技术分享:实现选择文件或目录路径的方法
17 2
|
8天前
|
前端开发 JavaScript Python
使用Python读取本地行情csv文件,做出web网页画出K线图实现案例
【5月更文挑战第4天】使用Python绘制K线图的步骤:1) 安装pandas, matplotlib和Flask;2) 用pandas读取CSV文件并处理数据;3) 创建Flask应用,渲染包含K线图数据的HTML;4) 编写HTML,使用ECharts库绘制K线图。
26 0
|
15天前
|
Linux iOS开发 MacOS
pyinstaller---Python代码的打包神器,一键将python代码打包成exe可执行文件
pyinstaller---Python代码的打包神器,一键将python代码打包成exe可执行文件