{"record":{"id":"17a51fe54c037762","repo":"Textualize/rich","slug":"invalid-mode-mode-r","errorCode":null,"errorMessage":"invalid mode {mode!r}","messagePattern":"invalid mode (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"rich/progress.py","lineNumber":1346,"sourceCode":"            mode (str): The mode to use to open the file. Only supports \"r\", \"rb\" or \"rt\".\n            buffering (int): The buffering strategy to use, see :func:`io.open`.\n            encoding (str, optional): The encoding to use when reading in text mode, see :func:`io.open`.\n            errors (str, optional): The error handling strategy for decoding errors, see :func:`io.open`.\n            newline (str, optional): The strategy for handling newlines in text mode, see :func:`io.open`.\n            total (int, optional): Total number of bytes to read. If none given, os.stat(path).st_size is used.\n            task_id (TaskID): Task to track. Default is new task.\n            description (str, optional): Description of task, if new task is created.\n\n        Returns:\n            BinaryIO: A readable file-like object in binary mode.\n\n        Raises:\n            ValueError: When an invalid mode is given.\n        \"\"\"\n        # normalize the mode (always rb, rt)\n        _mode = \"\".join(sorted(mode, reverse=False))\n        if _mode not in (\"br\", \"rt\", \"r\"):\n            raise ValueError(f\"invalid mode {mode!r}\")\n\n        # patch buffering to provide the same behaviour as the builtin `open`\n        line_buffering = buffering == 1\n        if _mode == \"br\" and buffering == 1:\n            warnings.warn(\n                \"line buffering (buffering=1) isn't supported in binary mode, the default buffer size will be used\",\n                RuntimeWarning,\n            )\n            buffering = -1\n        elif _mode in (\"rt\", \"r\"):\n            if buffering == 0:\n                raise ValueError(\"can't have unbuffered text I/O\")\n            elif buffering == 1:\n                buffering = -1\n\n        # attempt to get the total with `os.stat`\n        if total is None:\n            total = stat(file).st_size","sourceCodeStart":1328,"sourceCodeEnd":1364,"githubUrl":"https://github.com/Textualize/rich/blob/9d8f9a372cc5916fd4781fec207ced7ddac2f08f/rich/progress.py#L1328-L1364","documentation":"Progress.open() normalizes the mode string by sorting its characters and only accepts read modes that reduce to 'r', 'rb' (sorted 'br'), or 'rt'. Any other mode — including write/append modes like 'w', 'a', 'wb+', or unusual orderings like 'b+r' — raises ValueError('invalid mode ...') because this API exists solely to track read progress (downloads).","triggerScenarios":"progress.open('file.bin', 'wb'); progress.open('f.txt', 'w'); any mode containing w/a/x/+ characters, or a mode whose sorted form isn't in ('br','rt','r') such as 'b+r'.","commonSituations":"Copy-pasting an existing open(path, mode) call into progress.open() while keeping a write mode; assuming Progress.open mirrors the builtin open() semantics; trying to log/capture output into a progress-tracked file.","solutions":["Use a read mode: progress.open(path, 'rb') for binary or 'r'/'rt' for text.","For writing, use the builtin open() and update the progress task manually with progress.update(task_id, completed=f.tell()).","Double-check for typos/extra characters in the mode string (e.g. 'b+r' instead of 'rb')."],"exampleFix":"# before\nwith progress.open('out.bin', 'wb', total=n) as f:  # ValueError: invalid mode 'wb'\n    f.write(data)\n\n# after\nwith open('out.bin', 'wb') as f:\n    f.write(data)\n    progress.update(task_id, completed=f.tell())","handlingStrategy":"validation","validationCode":"VALID = ('r', 'rb', 'rt')\nmode = ''.join(sorted(mode))\nassert mode in VALID, f'Progress.open supports read modes only, got {mode!r}'","typeGuard":null,"tryCatchPattern":"try:\n    f = progress.open(path, mode)\nexcept ValueError as e:\n    if 'invalid mode' in str(e):\n        f = progress.open(path, 'rb')  # normalize to binary read\n    else:\n        raise","preventionTips":["Remember Progress.open is read-only by design — no 'w'/'a' modes.","Normalize mode strings before passing; only r/rb/rt survive the sorted-character check."],"tags":["rich","progress","file-mode","read-only","valueerror"],"backgroundTag":null,"analyzedSha":"9d8f9a372cc5916fd4781fec207ced7ddac2f08f","analyzedAt":"2026-08-15T03:30:11.781Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}