Python-os-fchown

提供:Dev Guides
2020年6月22日 (月) 22:35時点におけるMaintenance script (トーク | 投稿記録)による版 (Imported from text file)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先:案内検索

Python os.fchown()メソッド

説明

Pythonメソッド* fchown()*は、fdによって指定されたファイルの所有者とグループIDを数値のuidとgidに変更します。 IDの1つを変更しないままにするには、-1に設定します。

-このメソッドはPython 2.6以降で利用可能です。

構文

以下は* fchown()*メソッドの構文です-

os.fchown(fd, uid, gid);

パラメーター

  • fd -これは、所有者IDとグループIDを設定する必要があるファイル記述子です。
  • uid -これはファイルに設定される所有者IDです。
  • gid -これはファイルに設定されるグループIDです。

戻り値

このメソッドは値を返しません。

次の例は、fchown()メソッドの使用法を示しています。

#!/usr/bin/python

import os, sys, stat

# Now open a file "/tmp/foo.txt"
fd = os.open( "/tmp", os.O_RDONLY )

# Set the user Id to 100 for this file.
os.fchown( fd, 100, -1)

# Set the group Id to 50 for this file.
os.fchown( fd, -1, 50)

print "Changed ownership successfully!!"

# Close opened file.
os.close( fd )

上記のプログラムを実行すると、次の結果が生成されます-

Changed ownership successfully!!