Posts Tagged ‘attr’
Handle extended file attributes
February 6, 2024
Leave a comment
Problem
You want to handle extended file attributes from Python.
Solution
If you are not familiar with extended file attributes, you can read more about it here. I also have a blog post (see here), in which I show how to set/remove extended file attributes using the attr
Linux command.
To handle the extended file attributes from Python, you can use the pyxattr
package: https://github.com/iustin/pyxattr
Its usage is very simple:
>>> import xattr
>>> xattr.listxattr("main.py")
[b'user.com.dropbox.attrs']
>>> xattr.getxattr("main.py", "user.com.dropbox.attrs")
b'\n\x12...'
>>> xattr.setxattr("main.py", "user.com.dropbox.ignored", "1")
>>> xattr.listxattr("main.py")
[b'user.com.dropbox.attrs', b'user.com.dropbox.ignored']
>>> xattr.getxattr("main.py", "user.com.dropbox.ignored")
b'1'
>>> xattr.removexattr("main.py", "user.com.dropbox.ignored")
>>> xattr.listxattr("main.py")
[b'user.com.dropbox.attrs']
A typical use case: you want Dropbox to ignore your venv/
folder inside your project.
Links
- https://en.wikipedia.org/wiki/Extended_file_attributes
- https://help.dropbox.com/sync/ignored-files
- https://github.com/iustin/pyxattr (I used this in this post)
- https://github.com/xattr/xattr (an alternative)