Skip to content

Usage

py-file-attributes dynamically provides the correct handler based on your operating system.

Command Line Interface (CLI)

The package provides a built-in CLI command, file-attributes, which allows you to view or change file attributes directly from the terminal.

View attributes

To see all properties for a specific file, simply pass its path:

file-attributes path/to/file.txt

Change attributes

You can set or unset supported attributes using boolean flags:

file-attributes example.txt --hidden True
file-attributes example.txt --read_only False

Help Menu

To see which flags are available on your current operating system, use the --help flag. The help menu will display detailed descriptions for each available attribute:

file-attributes --help

Basic Operations

You can set and get common attributes that are available across all platforms.

from pathlib import Path
from file_attributes import FileAttributes

attrs = FileAttributes(Path("example.txt"))

# Read-only
attrs.set_read_only(True)
print(f"Is read-only: {attrs.read_only}")

# Hidden
attrs.set_hidden(True)
print(f"Is hidden: {attrs.hidden}")

# Check if it's a directory
print(f"Is directory: {attrs.directory}")

Platform-Specific Attributes

Some attributes only make sense on specific operating systems. Be careful when accessing these in cross-platform code, as they will raise an AttributeError if the attribute does not exist on the host OS.

Windows

# Archive
attrs.set_archive(True)
print(attrs.archive)

# Compressed
attrs.set_compressed(True)
print(attrs.compressed)

macOS / Linux

# Immutable
attrs.set_immutable(True)
print(attrs.immutable)

# Append Only
attrs.set_append_only(True)
print(attrs.append_only)

# No Dump
attrs.set_no_dump(True)
print(attrs.no_dump)