os.write(fd, str)
fd -- 這是檔案描述符
str -- 這是要寫入的字串
# !/usr/bin/python3 import os, sys # Open a file fd = os.open( "f1.txt", os.O_RDWR|os.O_CREAT ) # Write one string line="this is test" # string needs to be converted byte object b=str.encode(line) ret=os.write(fd, b) # ret consists of number of bytes written to f1.txt print ("the number of bytes written: ", ret) # Close opened file os.close( fd) print ("Closed the file successfully!!")
the number of bytes written: 12 Closed the file successfully!!