描述
方法ftruncate()截斷與文件描述符 fd 對應的文件,使其最多為 length 個字節。
用法
以下是語法ftruncate()方法 -
os.ftruncate(fd, length)
參數
fd- 這是文件描述符,需要截斷。
length- 這是需要截斷文件的文件長度。
返回值
此方法不返回任何值。在類 Unix 係統上可用。
示例
下麵的例子展示了 ftruncate() 方法的用法。
#!/usr/bin/python3
import os, sys
# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
# Write one string
os.write(fd, "This is test - This is test")
# Now you can use ftruncate() method.
os.ftruncate(fd, 10)
# Now read this file from the beginning.
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print ("Read String is:", str)
# Close opened file
os.close( fd )
print ("Closed the file successfully!!")
結果
當我們運行上述程序時,它會產生以下結果 -
Read String is: This is te Closed the file successfully!!
相關用法
- Python 3 os.fstatvfs()用法及代碼示例
- Python 3 os.fdopen()用法及代碼示例
- Python 3 os.fdatasync()用法及代碼示例
- Python 3 os.fchmod()用法及代碼示例
- Python 3 os.fchown()用法及代碼示例
- Python 3 os.fsync()用法及代碼示例
- Python 3 os.fpathconf()用法及代碼示例
- Python 3 os.fchdir()用法及代碼示例
- Python 3 os.fstat()用法及代碼示例
- Python 3 os.minor()用法及代碼示例
- Python 3 os.close()用法及代碼示例
- Python 3 os.unlink()用法及代碼示例
- Python 3 os.major()用法及代碼示例
- Python 3 os.rmdir()用法及代碼示例
- Python 3 os.isatty()用法及代碼示例
- Python 3 os.rename()用法及代碼示例
- Python 3 os.walk()用法及代碼示例
- Python 3 os.renames()用法及代碼示例
- Python 3 os.makedirs()用法及代碼示例
- Python 3 os.utime()用法及代碼示例
注:本文由純淨天空篩選整理自 Python 3 - os.ftruncate() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。