python学习之二

简介: 文件的操作:    打开文件的方式: >>> open('test.txt', 'w')>>> file('test.

文件的操作:

    打开文件的方式: 

>>> open('test.txt', 'w')
<open file 'test.txt', mode 'w' at 0x2b00cac7e6f0>
>>> file('test.txt', 'w')
<open file 'test.txt', mode 'w' at 0x2b00cac7e780>
>>> f = open ('test.txt', 'w')
>>> print f
<open file 'test.txt', mode 'w' at 0x2b00cac7e6f0>
      注: w -> 写操作  r->读操作  a-> 文本末尾添加  r+ ->可读可写 b->二进制读写  需要和w r 配合使用 比如:rb wb

   读文件操作 :

 1. 默认读完

>>> f = open('test1.txt', 'r')
>>> f.read()
 2. 逐行读取文件

>>> f = open('test1.txt', 'r')
>>> f.readline()

>>> f=open('test1.txt', 'r')
>>> f.readline()
'This is the first line in the file.\n'
>>> f.readline()
'this is the second line in the file.\n'
>>> f.readline()
'this is the third line in the file.\n'
>>> f.readline()
'this is the 4th line in the file.\n'
>>> f.readline()
'this is the 5th line in the file.\n'
>>> f.readline()
'this is the 6th line in the file.\n'
>>> f.readline()
'this is the 7th line in the file.\n'
>>> f.readline()
'this is the 8th line in the file.\n'
>>> f.readline()
'this is the 9th line in the file.\n'
>>> f.readline()
'this is the 10th line in the file.\n'
>>> f.readline()
'\n'
>>> f.readline()
''
 3. 读全文,系统自动以‘\n’结尾表示换行,并且每行作为list成员,以逗号分隔!

>>> f = open('test1.txt', 'r')
>>> f.readlines()
>>> f=open('test1.txt', 'r')
>>> f.readlines()
['This is the first line in the file.\n', 'this is the second line in the file.\n', 'this is the third line in the file.\n', 'this is the 4th line in the file.\n', 'this is the 5th line in the file.\n', 'this is the 6th line in the file.\n', 'this is the 7th line in the file.\n', 'this is the 8th line in the file.\n', 'this is the 9th line in the file.\n', 'this is the 10th line in the file.\n', '\n']

for 循环读取文件:

>>> f=open('test1.txt', 'r')
>>> for line in f:
...    print line
... 
This is the first line in the file.

this is the second line in the file.

this is the third line in the file.

this is the 4th line in the file.

this is the 5th line in the file.

this is the 6th line in the file.

this is the 7th line in the file.

this is the 8th line in the file.

this is the 9th line in the file.

this is the 10th line in the file.

以读写方式打开文件,查找偏移量对应的值并读出。

>>> f=open('test2.txt', 'r+')
>>> f.write('0123456789\n')
>>> f.seek(5)
>>> f.read(1)
'5'
>>> f.seek(-3, 2)
>>> f.read(1)
'8'







    

目录
相关文章
|
9天前
|
存储 Python
python基础篇:Python基础知识,帮助初学者快速入门
python基础篇:Python基础知识,帮助初学者快速入门
26 4
|
4月前
|
JSON 数据格式 Python
Python基础知识学习
Python基础知识学习
36 3
|
开发者 Python
|
应用服务中间件 开发者 Python
Python魔法方法的使用 | 手把手教你入门Python之五十三
Python ⾥有⼀种⽅法,叫做魔法⽅法。Python 的类⾥提供的,两个下划线开始,两个下划线结束的⽅法,就是魔法⽅法,魔法⽅法在恰当的时候就会被激活,⾃动执⾏。
Python魔法方法的使用 | 手把手教你入门Python之五十三
|
IDE 测试技术 Linux
编写Python代码 | 手把手教你入门Python之十
本节重点介绍编写Python代码
2202 0
编写Python代码 | 手把手教你入门Python之十
|
数据采集 网络协议 Linux
Python的使用场景 | 手把手教你入门Python之八
本节介绍了Python应用场景有哪些。
|
Unix Java 程序员
Python的发展史 | 手把手教你入门Python之七
Python就是一门解释型的编程语言,而且是现在世界上最流行的编程语言之一。
Python的发展史 | 手把手教你入门Python之七
|
存储 算法 索引
Python学习-基础知识-2
目录 Python基础知识2 一、二进制 二、文字编码-基础 为什么要有文字编码? 有哪些编码格式? 如何解决不同国家不兼容的编码格式? unicode编码格式的缺点 如何既能全球通用还可以规避unicode的缺点? python3的编码格式是什么样的? 三、浮点数 四、列表 如何理解列...
|
Java Shell Python
python学习-基础知识-1
1、计算机历史 计算机使用高低电压的两种状态来描述信息。计算机可以理解的只有二进制数据即010100011....,1个比特位可以表示的状态只有2种,n个比特位可以表示的状态有2的n次方种。 所以如果想要描述天气状态:天晴、下雨、刮风、下雪、霜冻,则需要使用3个比特位。