Python os.lseek() 方法

Python 教程 · 2019-02-04 05:58:56

概述

os.lseek() 方法用于设置文件描述符 fd 当前位置为 pos, how 方式修改。

在Unix,Windows中有效。

语法

lseek()方法语法格式如下:

os.lseek(fd, pos, how)

参数

  • fd -- 文件描述符。

  • pos -- 这是相对于给定的参数 how 在文件中的位置。。

  • how -- 文件内参考位置。SEEK_SET 或者 0 设置从文件开始的计算的pos; SEEK_CUR或者 1 则从当前位置计算; os.SEEK_END或者2则从文件尾部开始。

返回值

该方法没有返回值。

实例

以下实例演示了 lseek() 方法的使用:

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

import os, sys

# 打开文件
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# 写入字符串
os.write(fd, "This is test")

# 所有 fsync() 方法
os.fsync(fd)

# 从开始位置读取字符串
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print "Read String is : ", str

# 关闭文件
os.close( fd )

print "关闭文件成功!!"

执行以上程序输出结果为:

关闭文件成功!!

点击查看所有 Python 教程 文章: https://www.codercto.com/courses/l/8.html

查看所有标签

Defensive Design for the Web

Defensive Design for the Web

37signals、Matthew Linderman、Jason Fried / New Riders / 2004-3-2 / GBP 18.99

Let's admit it: Things will go wrong online. No matter how carefully you design a site, no matter how much testing you do, customers still encounter problems. So how do you handle these inevitable bre......一起来看看 《Defensive Design for the Web》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具