Python Glossary Русский

Python 3.3+Linuxosfunction

os.setxattr

os — extended attributes (xattr)

Set an extended attribute on a file.

Syntax

os.setxattr(...)

The name carries a namespace prefix ('user.tag') and the value is bytes. The XATTR_CREATE and XATTR_REPLACE flags make the write "create only" or "replace only". On filesystems without attribute support, such as some network shares and tmpfs on older kernels, OSError is raised.

Examples

import os
open('xattr_demo.txt', 'w').close()
os.setxattr('xattr_demo.txt', 'user.author', b'anna')   # → ? только Linux: атрибут записан, возвращает None (имя обязано начинаться с user.)
print(os.getxattr('xattr_demo.txt', 'user.author'))   # → b'anna'
print(os.setxattr('xattr_demo.txt', 'user.author', b'clara', os.XATTR_CREATE))   # → OSError (EEXIST) — с flags=0 значение просто перезаписалось бы
print(os.setxattr('xattr_demo.txt', 'user.tag', 'важное'))   # → TypeError — значение должно быть bytes, а не str

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy