Python-os-dup2
提供:Dev Guides
Python os.dup2()メソッド
説明
Pythonメソッド* dup2()*は、ファイル記述子_fd_を_fd2_に複製し、必要に応じて後者を最初に閉じます。
注-新しいファイルの説明は、利用可能な場合にのみ割り当てられます。 以下に示す次の例では、1000が使用可能な場合、1000が重複fdとして割り当てられます。
構文
以下は* dup2()*メソッドの構文です-
os.dup2(fd, fd2);
パラメーター
- fd -これは複製されるファイル記述子です。
- fd2 -これは重複ファイル記述子です。
戻り値
このメソッドは、ファイル記述子の複製を返します。
例
次の例は、dup2()メソッドの使用法を示しています。
#!/usr/bin/python
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")
# Now duplicate this file descriptor as 1000
fd2 = 1000
os.dup2(fd, fd2);
# Now read this file from the beginning using fd2.
os.lseek(fd2, 0, 0)
str = os.read(fd2, 100)
print "Read String is : ", str
# Close opened file
os.close( fd )
print "Closed the file successfully!!"
上記のプログラムを実行すると、次の結果が生成されます-
Read String is : This is test
Closed the file successfully!!