首页 > Python资料 博客日记
Python os.lseek()方法
2023-08-08 16:45:12Python资料围观210次
Python资料网推荐Python os.lseek()方法这篇文章给大家,欢迎收藏Python资料网享受知识的乐趣
Python的lseek()
方法将文件描述符fd
的当前位置设置为给定位置pos
,由how
指定如何修改。
语法
以下是lseek()
方法的语法 -
os.lseek(fd, pos, how)
参数
- fd - 这是文件描述符,需要处理。
- pos - 这是相对于给定参数文件的位置。
os.SEEK_SET
或0
设置相对于文件开头的位置,os.SEEK_CUR
或1
用来设置它相对于当前位置;os.SEEK_END
或2
用来设置它相对于文件的结尾。 - how - 这是文件中的参考点。
os.SEEK_SET
或0
表示文件的开头,os.SEEK_CUR
或1
表示当前位置,os.SEEK_END
或2
表示文件的结尾。
定义的pos
常数 -
os.SEEK_SET = 0
os.SEEK_CUR = 1
os.SEEK_END = 2
返回值
- 此方法不返回任何值。
示例
以下示例显示了lseek()
方法的用法。
#!/usr/bin/python3
import os, sys
# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
# Write one string
line = "This is test"
b = line.encode()
os.write(fd, b)
# Now you can use fsync() method.
# Infact here you would not be able to see its effect.
os.fsync(fd)
# Now read this file from the beginning
os.lseek(fd, 0, 0)
line = os.read(fd, 100)
print ("Read String is : ", line.decode())
# Close opened file
os.close( fd )
print "Closed the file successfully!!"
执行上面代码后,将得到以下结果 -
Read String is : This is test
Closed the file successfully!!
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
上一篇:Python线性回归
下一篇:Python os.rename()方法
相关文章
最新发布
- import torch 报错:WinError 126
- 如何在Java中实现邮件发送功能?
- python之openpyxl模块——实现Excel表格的处理(万字教学,全网最全,超详细!)
- BUG解决:postman可以请求成功,但Python requests请求报403
- 解决Pycharm下载torch报错:OSError: [WinError 126] 找不到指定的模块。
- python创建线程和结束线程
- 精通Python(基础篇)pip命令大全(非常详细)零基础入门到精通,收藏这一篇就够了
- Python中的列表(list)
- ImportError: /usr/local/lib/python3.8/dist-packages/flash_attn_2_cuda.cpython-38-x86_64-linux-gnu.so
- Python实现水果忍者(开源)
点击排行
- 版本匹配指南:Numpy版本和Python版本的对应关系
- Python 可视化 web 神器:streamlit、Gradio、dash、nicegui;低代码 Python Web 框架:PyWebIO
- 版本匹配指南:PyTorch版本、torchvision 版本和Python版本的对应关系
- 相关性分析——Pearson相关系数+热力图(附data和Python完整代码)
- Could not build wheels for llama-cpp-python, which is required to install pyproject.toml-based proj
- Python pyinstaller打包exe最完整教程
- Anaconda版本和Python版本对应关系(持续更新...)
- Python与PyTorch的版本对应