{"record":{"id":"3a0c97ac56b5346c","repo":"python/cpython","slug":"binary-mode-doesn-t-take-a-newline-argument","errorCode":null,"errorMessage":"binary mode doesn't take a newline argument","messagePattern":"binary mode doesn't take a newline argument","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/_pyio.py","lineNumber":227,"sourceCode":"    creating = \"x\" in modes\n    reading = \"r\" in modes\n    writing = \"w\" in modes\n    appending = \"a\" in modes\n    updating = \"+\" in modes\n    text = \"t\" in modes\n    binary = \"b\" in modes\n    if text and binary:\n        raise ValueError(\"can't have text and binary mode at once\")\n    if creating + reading + writing + appending > 1:\n        raise ValueError(\"can't have read/write/append mode at once\")\n    if not (creating or reading or writing or appending):\n        raise ValueError(\"must have exactly one of read/write/append mode\")\n    if binary and encoding is not None:\n        raise ValueError(\"binary mode doesn't take an encoding argument\")\n    if binary and errors is not None:\n        raise ValueError(\"binary mode doesn't take an errors argument\")\n    if binary and newline is not None:\n        raise ValueError(\"binary mode doesn't take a newline argument\")\n    if binary and buffering == 1:\n        import warnings\n        warnings.warn(\"line buffering (buffering=1) isn't supported in binary \"\n                      \"mode, the default buffer size will be used\",\n                      RuntimeWarning, 2)\n    raw = FileIO(file,\n                 (creating and \"x\" or \"\") +\n                 (reading and \"r\" or \"\") +\n                 (writing and \"w\" or \"\") +\n                 (appending and \"a\" or \"\") +\n                 (updating and \"+\" or \"\"),\n                 closefd, opener=opener)\n    result = raw\n    try:\n        line_buffering = False\n        if buffering == 1 or buffering < 0 and raw._isatty_open_only():\n            buffering = -1\n            line_buffering = True","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pyio.py#L209-L245","documentation":"Raised by io.open() as a ValueError when binary mode ('b') is combined with a non-None newline argument. newline controls universal-newline translation for text streams; binary streams return bytes verbatim, so newline translation cannot apply.","triggerScenarios":"open('f.bin', 'rb', newline='\\n'), or wrappers forwarding newline='' unconditionally (a very common idiom for CSV files: open(p, newline='') — which is only valid in text mode).","commonSituations":"CSV/record parsing code where newline='' is cargo-culted onto every open() and then someone switches the mode to 'rb' (e.g. to use csv with bytes or to hash a file); cross-platform line-ending work ported from text to binary.","solutions":["Drop newline for binary opens — bytes are returned untranslated by definition.","In shared helpers, pass newline only in text mode.","If you were using newline='' for csv.reader, keep the file in text mode ('r', encoding=...) — csv accepts text streams."],"exampleFix":"// before\nwith open('data.csv', 'rb', newline='') as f:   # ValueError\n    rows = list(csv.reader(f))\n\n// after\nwith open('data.csv', 'r', encoding='utf-8', newline='') as f:\n    rows = list(csv.reader(f))\n# binary alternative (no newline kwarg):\nwith open('data.csv', 'rb') as f:\n    raw = f.read()","handlingStrategy":"validation","validationCode":"def open_any(path, mode='r', newline=None, **kw):\n    if 'b' in mode:\n        newline = None\n    return open(path, mode, newline=newline, **kw)","typeGuard":"def newline_allowed(mode: str) -> bool:\n    return 'b' not in mode","tryCatchPattern":null,"preventionTips":["newline='' is a text-mode (CSV) idiom — never copy it onto 'rb' opens.","Keep CSV reading in text mode with an explicit encoding; hash/copy loops use plain 'rb' with no newline kwarg.","Centralize open() calls behind one helper that gates text-only kwargs on the mode."],"tags":["io","open","newline","binary-io","valueerror","csv"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}