diff --git a/pyupgrade.py b/pyupgrade.py
index a49ef68..45a13bb 100644
--- a/pyupgrade.py
+++ b/pyupgrade.py
@@ -1127,7 +1127,7 @@ SIX_NATIVE_STR = frozenset(('ensure_str', 'ensure_text', 'text_type'))
 U_MODE_REMOVE = frozenset(('U', 'Ur', 'rU', 'r', 'rt', 'tr'))
 U_MODE_REPLACE_R = frozenset(('Ub', 'bU'))
 U_MODE_REMOVE_U = frozenset(('rUb', 'Urb', 'rbU', 'Ubr', 'bUr', 'brU'))
-U_MODE_ALL = U_MODE_REMOVE | U_MODE_REPLACE_R | U_MODE_REMOVE_U
+U_MODE_REPLACE = U_MODE_REPLACE_R | U_MODE_REMOVE_U
 
 
 def _all_isinstance(
@@ -1570,10 +1570,12 @@ class FindPy3Plus(ast.NodeVisitor):
         elif (
                 isinstance(node.func, ast.Name) and
                 node.func.id == 'open' and
-                len(node.args) >= 2 and
                 not _starargs(node) and
-                isinstance(node.args[1], ast.Str) and
-                node.args[1].s in U_MODE_ALL
+                len(node.args) >= 2 and
+                isinstance(node.args[1], ast.Str) and (
+                    node.args[1].s in U_MODE_REPLACE or
+                    (len(node.args) == 2 and node.args[1].s in U_MODE_REMOVE)
+                )
         ):
             self.open_mode_calls.add(_ast_to_offset(node))
         elif (

open() rewrite doesn't always consider other positional args
Minimal input:

```py
with open('blah.txt', 'r', 'utf-8') as fp:
    pass
```

Actual output:

```py
with open('blah.txt', 'utf-8') as fp:
    pass
```

Expected:

```py
with open('blah.txt', encoding='utf-8') as fp:
    pass
```

The rewrite causes the following Python error upon executing the code:

```
Traceback (most recent call last):
  File "blah.py", line 1, in <module>
    with open('blah.txt', 'utf-8') as fp:
ValueError: invalid mode: 'utf-8'
```

Discovered in a real code base:

https://github.com/andialbrecht/sqlparse/blob/28a2acdd9b307c95d2111f1f831b1a5afc634691/sqlparse/cli.py#L175

diff --git a/tests/open_mode_test.py b/tests/open_mode_test.py
index dd306a8..1398792 100644
--- a/tests/open_mode_test.py
+++ b/tests/open_mode_test.py
@@ -13,6 +13,8 @@ from pyupgrade import _fix_py3_plus
         'open("foo", "Uw")',
         # TODO: could maybe be rewritten to remove t?
         'open("foo", "wt")',
+        # don't remove this, they meant to use `encoding=`
+        'open("foo", "r", "utf-8")',
     ),
 )
 def test_fix_open_mode_noop(s):

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest pre-commit
3.9
requirements-dev.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
cfgv==3.4.0
covdefaults==2.3.0
coverage==7.8.0
distlib==0.3.9
exceptiongroup==1.2.2
filelock==3.18.0
identify==2.6.9
iniconfig==2.1.0
nodeenv==1.9.1
packaging==24.2
platformdirs==4.3.7
pluggy==1.5.0
pre_commit==4.2.0
pytest==8.3.5
-e git+https://github.com/asottile/pyupgrade.git@06444be5513ab77a149b7b4ae44d51803561e36f#egg=pyupgrade
PyYAML==6.0.2
tokenize_rt==6.1.0
tomli==2.2.1
virtualenv==20.29.3

name: pyupgrade
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - cfgv==3.4.0
      - covdefaults==2.3.0
      - coverage==7.8.0
      - distlib==0.3.9
      - exceptiongroup==1.2.2
      - filelock==3.18.0
      - identify==2.6.9
      - iniconfig==2.1.0
      - nodeenv==1.9.1
      - packaging==24.2
      - platformdirs==4.3.7
      - pluggy==1.5.0
      - pre-commit==4.2.0
      - pytest==8.3.5
      - pyyaml==6.0.2
      - tokenize-rt==6.1.0
      - tomli==2.2.1
      - virtualenv==20.29.3
prefix: /opt/conda/envs/pyupgrade

tests/open_mode_test.py::test_fix_open_mode_noop[open("foo",
tests/open_mode_test.py::test_fix_open_mode[open("foo", tests/open_mode_test.py::test_fix_open_mode[open("f",
pyupgrade.py
pyupgrade.py