Python File 方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shelbaydo/article/details/84672855

Python File fileno() 方法

fileno() 方法返回一个整型的文件描述符(file descriptor FD 整型),可用于底层操作系统的 I/O 操作。

fileObject.fileno(); 
#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 打开文件
fo = open("runoob.txt", "wb")
print "文件名为: ", fo.name

fid = fo.fileno()
print "文件描述符为: ", fid

# 关闭文件
fo.close()
文件名为:  runoob.txt
文件描述符为:  3

Python File isatty() 方法

isatty() 方法检测文件是否连接到一个终端设备,如果是返回 True,否则返回 False。

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 打开文件
fo = open("runoob.txt", "wb")
print "文件名为: ", fo.name

ret = fo.isatty()
print "返回值 : ", ret

# 关闭文件
fo.close()
文件名为:  runoob.txt
返回值 :  False

Python File next() 方法

next() 方法在文件使用迭代器时会使用到,在循环中,next()方法会在每次循环中调用,该方法返回文件的下一行,如果到达结尾(EOF),则触发 StopIteration
循环读取文件的内容:
文件 runoob.txt 的内容如下:

这是第一行
这是第二行
这是第三行
这是第四行
这是第五行

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
# 打开文件
fo = open("runoob.txt", "r+")
print "文件名为: ", fo.name
 
for index in range(5):
    line = fo.next()
    print "第 %d 行 - %s" % (index, line)
 
# 关闭文件
fo.close()
文件名为:  runoob.txt
第 0 行 - 这是第一行

第 1 行 - 这是第二行

第 2 行 - 这是第三行

第 3 行 - 这是第四行

第 4 行 - 这是第五行

range(1,5) #代表从1到5(不包含5):[1, 2, 3, 4]
range(1,5,2) #代表从1到5,间隔2(不包含5):[1, 3]
range(5) #代表从0到5(不包含5):[0, 1, 2, 3, 4]

Python File readline() 方法

以下实例演示了 readline() 方法的使用:
文件 runoob.txt 的内容如下:

1:www.runoob.com
2:www.runoob.com
3:www.runoob.com
4:www.runoob.com
5:www.runoob.com

循环读取文件的内容:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 打开文件
fo = open("runoob.txt", "rw+")
print "文件名为: ", fo.name

line = fo.readline()
print "读取第一行 %s" % (line)

line = fo.readline(5)
print "读取的字符串为: %s" % (line)

# 关闭文件
fo.close()

运行结果

文件名为:  runoob.txt
读取第一行 1:www.runoob.com

读取的字符串为: 2:www

Python File readlines() 方法

readlines() 方法用于读取所有行(直到结束符 EOF)并返回列表,该列表可以由 Python 的 for… in … 结构进行处理。
如果碰到结束符 EOF 则返回空字符串。

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
# 打开文件
fo = open("runoob.txt", "r")
print "文件名为: ", fo.name
 
for line in fo.readlines():                          #依次读取每行  
    line = line.strip()                             #去掉每行头尾空白  
    print "读取的数据为: %s" % (line)
 
# 关闭文件
fo.close()

以上实例输出结果为:

文件名为:  runoob.txt
读取的数据为: 1:www.runoob.com
读取的数据为: 2:www.runoob.com
读取的数据为: 3:www.runoob.com
读取的数据为: 4:www.runoob.com
读取的数据为: 5:www.runoob.com

Python File truncate() 方法

truncate() 方法用于截断文件,如果指定了可选参数 size,则表示截断文件为 size 个字符。 如果没有指定 size,则从当前位置起截断;截断之后 size 后面的所有字符被删除。
以下实例演示了 truncate() 方法的使用:
文件 runoob.txt 的内容如下:

扫描二维码关注公众号,回复: 5391738 查看本文章
1:www.runoob.com
2:www.runoob.com
3:www.runoob.com
4:www.runoob.com
5:www.runoob.com

循环读取文件的内容:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 打开文件
fo = open("runoob.txt", "r+")
print "文件名为: ", fo.name

line = fo.readline()
print "读取第一行: %s" % (line)

# 截断剩下的字符串
fo.truncate()

# 尝试再次读取数据
line = fo.readline()
print "读取数据: %s" % (line)

# 关闭文件
fo.close()

以上实例输出结果为:

文件名为:  runoob.txt
读取第一行: 1:www.runoob.com

读取数据:

以下实例截取 runoob.txt 文件的10个字节:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 打开文件
fo = open("runoob.txt", "r+")
print "文件名为: ", fo.name

# 截取10个字节
fo.truncate(10)

str = fo.read()
print "读取数据: %s" % (str)

# 关闭文件
fo.close()

以上实例输出结果为:

文件名为:  runoob.txt
读取数据: 1:www.runo**

Python File writelines() 方法

writelines() 方法用于向文件中写入一序列的字符串。
这一序列字符串可以是由迭代对象产生的,如一个字符串列表。
换行需要制定换行符 \n。
以下实例演示了 writelines() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 打开文件
fo = open("test.txt", "w")
print "文件名为: ", fo.name
seq = ["菜鸟教程 1\n", "菜鸟教程 2"]
fo.writelines( seq )

# 关闭文件
fo.close()

以上实例输出结果为:

文件名为:  test.txt

查看文件内容:

$ cat test.txt
菜鸟教程 1
菜鸟教程 2

猜你喜欢

转载自blog.csdn.net/shelbaydo/article/details/84672855