2017-10-06 5 views
1
import os,sys 

sr=os.open("sri.txt",os.O_RDWR|os.O_CREAT) 

os.write(sr,"This is the test-This is test") 

os.ftruncate(sr,10) 

os.lseek(sr,0,0) 

str=os.read(sr,100) 

print("Read string is:",str) 

os.close(sr) 

print("closed the file successfully!!") 
+0

の下に、私は 'ftruncate'は' write'エラーを発生させるとされていないことを信じる多くの問題を抱えていますに見えます。 –

答えて

0

すべての文字列は自動的にUnicodeです。

私はそれを修正する2つの方法を見ることができます。

以下に示すように、文字列を書き込む前に "b"を付加します。

os.write(sr,b"This is the test-This is test") 

または

別下記のように呼び出しコード()です。 python3内の文字列の挙動をより良く理解するため

str = "This is the test-This is test" 

ret = os.write(sr,str.encode()) 

はチュートリアル

http://pythoncentral.io/encoding-and-decoding-strings-in-python-3-x/

関連する問題