Repository: yt-dlp
Issue: Safer overwriting of cookies.txt
Labels: enhancement,core:cookies

### DO NOT REMOVE OR SKIP THE ISSUE TEMPLATE

- [X] I understand that I will be **blocked** if I remove or skip any mandatory\* field

### Checklist

- [X] I'm requesting a feature unrelated to a specific site
- [X] I've looked through the [README](https://github.com/yt-dlp/yt-dlp#readme)
- [X] I've verified that I'm running yt-dlp version **2023.01.02** ([update instructions](https://github.com/yt-dlp/yt-dlp#update)) or later (specify commit)
- [X] I've searched the [bugtracker](https://github.com/yt-dlp/yt-dlp/issues?q=) for similar issues **including closed ones**. DO NOT post duplicates
- [X] I've read the [guidelines for opening an issue](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#opening-an-issue)

### Provide a description that is worded well enough to be understood

The only way to pass cookies from a `cookies.txt` file to a request is to use the `--cookies` flag;
however, `--cookies` not only reads from the file but also writes new cookies to it:

```
--cookies FILE                  Netscape formatted file to read cookies from
                                and dump cookie jar in
```

[This is expected behavior](https://github.com/ytdl-org/youtube-dl/issues/16505), but it can often get in the way,
such as when the same `cookies.txt` file is needs to reused multiple times.

It does not currently seem possible to achieve this, save by making `cookies.txt` read-only,
but that obviously isn't a good workaround and leads to a ton of other issues,
such as `yt-dlp` throwing an exception and exiting.

### Provide verbose output that clearly demonstrates the problem

- [X] Run **your** yt-dlp command with **-vU** flag added (`yt-dlp -vU <your command line>`)
- [X] Copy the WHOLE output (starting with `[debug] Command-line config`) and insert it below

### Complete Verbose Output

```shell
Standard output:

Latest version: 2023.01.02, Current version: 2023.01.02
yt-dlp is up to date (2023.01.02)
[youtube] Extracting URL: dQw4w9WgXcQ
[youtube] dQw4w9WgXcQ: Downloading webpage
[youtube] dQw4w9WgXcQ: Downloading android player API JSON
[info] dQw4w9WgXcQ: Downloading 1 format(s): 248+251
[download] Destination: Rick Astley - Never Gonna Give You Up (Official Music Video) [dQw4w9WgXcQ].f248.webm

[download]   0.0% of   53.59MiB at  Unknown B/s ETA Unknown
[download]   0.0% of   53.59MiB at  Unknown B/s ETA 00:18  
[download]   0.0% of   53.59MiB at    5.03MiB/s ETA 00:10
[download]   0.0% of   53.59MiB at    8.35MiB/s ETA 00:06
[download]   0.1% of   53.59MiB at   12.02MiB/s ETA 00:04
[download]   0.1% of   53.59MiB at   14.63MiB/s ETA 00:03
[download]   0.2% of   53.59MiB at   20.58MiB/s ETA 00:02
[download]   0.5% of   53.59MiB at   26.89MiB/s ETA 00:01
[download]   0.9% of   53.59MiB at   32.59MiB/s ETA 00:01
[download]   1.9% of   53.59MiB at   36.31MiB/s ETA 00:01
[download]   3.7% of   53.59MiB at   25.85MiB/s ETA 00:01
[download]   7.5% of   53.59MiB at   22.82MiB/s ETA 00:02
[download]  14.9% of   53.59MiB at   21.49MiB/s ETA 00:02
[download]  18.4% of   53.59MiB at   21.50MiB/s ETA 00:02
[download]  18.4% of   53.59MiB at  Unknown B/s ETA Unknown
[download]  18.4% of   53.59MiB at  Unknown B/s ETA Unknown
[download]  18.4% of   53.59MiB at    5.98MiB/s ETA 00:07  
[download]  18.4% of   53.59MiB at    9.48MiB/s ETA 00:04
[download]  18.4% of   53.59MiB at   13.15MiB/s ETA 00:03
[download]  18.5% of   53.59MiB at   18.00MiB/s ETA 00:02
[download]  18.6% of   53.59MiB at   23.61MiB/s ETA 00:01
[download]  18.8% of   53.59MiB at   29.19MiB/s ETA 00:01
[download]  19.3% of   53.59MiB at   34.22MiB/s ETA 00:01
[download]  20.2% of   53.59MiB at   35.16MiB/s ETA 00:01
[download]  22.1% of   53.59MiB at   25.71MiB/s ETA 00:01
[download]  25.8% of   53.59MiB at   22.72MiB/s ETA 00:01
[download]  33.3% of   53.59MiB at   21.45MiB/s ETA 00:01
[download]  36.9% of   53.59MiB at   21.48MiB/s ETA 00:01
[download]  36.9% of   53.59MiB at  Unknown B/s ETA Unknown
[download]  36.9% of   53.59MiB at  Unknown B/s ETA Unknown
[download]  36.9% of   53.59MiB at    5.27MiB/s ETA 00:06  
[download]  36.9% of   53.59MiB at    8.63MiB/s ETA 00:03
[download]  37.0% of   53.59MiB at   12.35MiB/s ETA 00:02
[download]  37.0% of   53.59MiB at   17.23MiB/s ETA 00:01
[download]  37.1% of   53.59MiB at   22.91MiB/s ETA 00:01
[download]  37.4% of   53.59MiB at   28.10MiB/s ETA 00:01
[download]  37.8% of   53.59MiB at   33.51MiB/s ETA 00:00
[download]  38.8% of   53.59MiB at   26.15MiB/s ETA 00:01
[download]  40.6% of   53.59MiB at   25.33MiB/s ETA 00:01
[download]  44.4% of   53.59MiB at   22.56MiB/s ETA 00:01
[download]  51.8% of   53.59MiB at   21.39MiB/s ETA 00:01
[download]  54.7% of   53.59MiB at   21.47MiB/s ETA 00:01
[download]  54.7% of   53.59MiB at  Unknown B/s ETA Unknown
[download]  54.7% of   53.59MiB at  Unknown B/s ETA Unknown
[download]  54.7% of   53.59MiB at    5.89MiB/s ETA 00:04  
[download]  54.7% of   53.59MiB at    9.37MiB/s ETA 00:02
[download]  54.8% of   53.59MiB at   13.03MiB/s ETA 00:01
[download]  54.8% of   53.59MiB at   17.12MiB/s ETA 00:01
[download]  54.9% of   53.59MiB at   22.81MiB/s ETA 00:01
[download]  55.2% of   53.59MiB at   28.62MiB/s ETA 00:00
[download]  55.6% of   53.59MiB at   33.92MiB/s ETA 00:00
[download]  56.6% of   53.59MiB at   35.11MiB/s ETA 00:00
[download]  58.4% of   53.59MiB at   25.91MiB/s ETA 00:00
[download]  62.2% of   53.59MiB at   22.73MiB/s ETA 00:00
[download]  69.6% of   53.59MiB at   21.45MiB/s ETA 00:00
[download]  73.0% of   53.59MiB at   21.48MiB/s ETA 00:00
[download]  73.0% of   53.59MiB at  Unknown B/s ETA Unknown
[download]  73.0% of   53.59MiB at  Unknown B/s ETA Unknown
[download]  73.0% of   53.59MiB at    5.98MiB/s ETA 00:02  
[download]  73.1% of   53.59MiB at    9.43MiB/s ETA 00:01
[download]  73.1% of   53.59MiB at   13.10MiB/s ETA 00:01
[download]  73.1% of   53.59MiB at   16.15MiB/s ETA 00:00
[download]  73.3% of   53.59MiB at   21.96MiB/s ETA 00:00
[download]  73.5% of   53.59MiB at   27.87MiB/s ETA 00:00
[download]  74.0% of   53.59MiB at   33.31MiB/s ETA 00:00
[download]  74.9% of   53.59MiB at   35.78MiB/s ETA 00:00
[download]  76.8% of   53.59MiB at   23.15MiB/s ETA 00:00
[download]  80.5% of   53.59MiB at   22.80MiB/s ETA 00:00
[download]  88.0% of   53.59MiB at   21.49MiB/s ETA 00:00
[download]  91.4% of   53.59MiB at   21.51MiB/s ETA 00:00
[download]  91.5% of   53.59MiB at  Unknown B/s ETA Unknown
[download]  91.5% of   53.59MiB at  Unknown B/s ETA Unknown
[download]  91.5% of   53.59MiB at    6.07MiB/s ETA 00:00  
[download]  91.5% of   53.59MiB at    9.59MiB/s ETA 00:00
[download]  91.5% of   53.59MiB at   12.31MiB/s ETA 00:00
[download]  91.6% of   53.59MiB at   13.51MiB/s ETA 00:00
[download]  91.7% of   53.59MiB at   19.36MiB/s ETA 00:00
[download]  91.9% of   53.59MiB at   25.73MiB/s ETA 00:00
[download]  92.4% of   53.59MiB at   31.76MiB/s ETA 00:00
[download]  93.3% of   53.59MiB at   20.33MiB/s ETA 00:00
[download]  95.2% of   53.59MiB at   25.21MiB/s ETA 00:00
[download]  98.9% of   53.59MiB at   22.74MiB/s ETA 00:00
[download] 100.0% of   53.59MiB at   22.70MiB/s ETA 00:00
[download] 100% of   53.59MiB in 00:00:02 at 18.75MiB/s  
[download] Destination: Rick Astley - Never Gonna Give You Up (Official Music Video) [dQw4w9WgXcQ].f251.weba

[download]   0.0% of    3.28MiB at  Unknown B/s ETA Unknown
[download]   0.1% of    3.28MiB at  Unknown B/s ETA Unknown
[download]   0.2% of    3.28MiB at    5.18MiB/s ETA 00:00  
[download]   0.4% of    3.28MiB at    8.53MiB/s ETA 00:00
[download]   0.9% of    3.28MiB at   12.18MiB/s ETA 00:00
[download]   1.9% of    3.28MiB at   13.76MiB/s ETA 00:00
[download]   3.8% of    3.28MiB at   19.57MiB/s ETA 00:00
[download]   7.6% of    3.28MiB at   25.77MiB/s ETA 00:00
[download]  15.2% of    3.28MiB at   31.73MiB/s ETA 00:00
[download]  30.5% of    3.28MiB at   36.05MiB/s ETA 00:00
[download]  61.0% of    3.28MiB at   23.84MiB/s ETA 00:00
[download] 100.0% of    3.28MiB at   23.56MiB/s ETA 00:00
[download] 100% of    3.28MiB in 00:00:00 at 20.43MiB/s  
[Merger] Merging formats into "Rick Astley - Never Gonna Give You Up (Official Music Video) [dQw4w9WgXcQ].webm"
Deleting original file Rick Astley - Never Gonna Give You Up (Official Music Video) [dQw4w9WgXcQ].f248.webm (pass -k to keep)
Deleting original file Rick Astley - Never Gonna Give You Up (Official Music Video) [dQw4w9WgXcQ].f251.weba (pass -k to keep)

Standard error:

[debug] Command-line config: ['-vU', '--cookies', 'cookies.txt', 'dQw4w9WgXcQ']
[debug] User config: []
[debug] System config: []
[debug] Encodings: locale UTF-8, fs utf-8, pref UTF-8, out utf-8 (No ANSI), error utf-8 (No ANSI), screen utf-8 (No ANSI)
[debug] yt-dlp version 2023.01.02 [d83b0ad]
[debug] Python 3.10.9 (CPython x86_64 64bit) - Linux-5.19.12-arch1-1-x86_64-with-glibc2.36 (OpenSSL 3.0.7 1 Nov 2022, glibc 2.36)
[debug] exe versions: ffmpeg 5.1.2 (setts), ffprobe 5.1.2
[debug] Optional libraries: Cryptodome-3.12.0, brotli-1.0.9, certifi-2022.12.07, mutagen-1.46.0, sqlite3-2.6.0, websockets-10.4
[debug] Proxy map: {}
[debug] Loaded 1754 extractors
[debug] Fetching release info: https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest
[debug] Sort order given by extractor: quality, res, fps, hdr:12, source, vcodec:vp9.2, channels, acodec, lang, proto
[debug] Formats sorted by: hasvid, ie_pref, quality, res, fps, hdr:12(7), source, vcodec:vp9.2(10), channels, acodec, lang, proto, filesize, fs_approx, tbr, vbr, abr, asr, vext, aext, hasaud, id
[debug] Default format spec: bestvideo*+bestaudio/best
[debug] Invoking http downloader on "https://rr3---sn-uxa0n-t8gy.googlevideo.com/videoplayback?expire=1673032798&ei=_h-4Y7Vah6KTBsC1v_gG&ip=2001%3A569%3A52ad%3A4300%3Aca54%3A2c5b%3Ad810%3A30a1&id=o-AIUXQUJrKqeYtftHYiozSF1EJtd4t_YqzTFjYS2PKxYb&itag=248&source=youtube&requiressl=yes&mh=7c&mm=31%2C26&mn=sn-uxa0n-t8gy%2Csn-vgqsrnld&ms=au%2Conr&mv=m&mvi=3&pl=44&initcwndbps=2168750&spc=zIddbCE6XvG7igmbVrBP8ttQEJiHokI&vprv=1&svpuc=1&mime=video%2Fwebm&gir=yes&clen=56193418&dur=212.040&lmt=1665678637466127&mt=1673010844&fvip=4&keepalive=yes&fexp=24007246&c=ANDROID&txp=453D434&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cspc%2Cvprv%2Csvpuc%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRQIhAO6AKHFnXBy5MLWoS_kxR2JV4ftWqKzz5mS5ZSr7HWpBAiAORsWDVZ_5ulukGhIXKFstSurh9drD3fhkW4B6OPeBQg%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRQIgb2ledIkLnF2tNiKgT2DetUOVSqE5_fxNLLNCZK1L9EICIQC5MMBMrhFzhw6epfqrytelntaXaUgajefkTrtx2JdADQ%3D%3D"
[debug] Invoking http downloader on "https://rr3---sn-uxa0n-t8gy.googlevideo.com/videoplayback?expire=1673032798&ei=_h-4Y7Vah6KTBsC1v_gG&ip=2001%3A569%3A52ad%3A4300%3Aca54%3A2c5b%3Ad810%3A30a1&id=o-AIUXQUJrKqeYtftHYiozSF1EJtd4t_YqzTFjYS2PKxYb&itag=251&source=youtube&requiressl=yes&mh=7c&mm=31%2C26&mn=sn-uxa0n-t8gy%2Csn-vgqsrnld&ms=au%2Conr&mv=m&mvi=3&pl=44&initcwndbps=2168750&spc=zIddbCE6XvG7igmbVrBP8ttQEJiHokI&vprv=1&svpuc=1&mime=audio%2Fwebm&gir=yes&clen=3437753&dur=212.061&lmt=1651669822191078&mt=1673010844&fvip=4&keepalive=yes&fexp=24007246&c=ANDROID&txp=4532434&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cspc%2Cvprv%2Csvpuc%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRQIhAINqWuKbg5UyoIeQtFoRGdqlbDHiC3zo_obfEYK7kfDmAiAVE62LXHgHfyUVV-tGqsJrSeUPClrFEtfVSC7kffjs_A%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRQIgb2ledIkLnF2tNiKgT2DetUOVSqE5_fxNLLNCZK1L9EICIQC5MMBMrhFzhw6epfqrytelntaXaUgajefkTrtx2JdADQ%3D%3D"
[debug] ffmpeg command line: ffmpeg -y -loglevel repeat+info -i 'file:Rick Astley - Never Gonna Give You Up (Official Music Video) [dQw4w9WgXcQ].f248.webm' -i 'file:Rick Astley - Never Gonna Give You Up (Official Music Video) [dQw4w9WgXcQ].f251.weba' -c copy -map 0:v:0 -map 1:a:0 -movflags +faststart 'file:Rick Astley - Never Gonna Give You Up (Official Music Video) [dQw4w9WgXcQ].temp.webm'
Traceback (most recent call last):
  File "/usr/bin/yt-dlp", line 8, in <module>
    sys.exit(main())
  File "/usr/lib/python3.10/site-packages/yt_dlp/__init__.py", line 970, in main
    _exit(*variadic(_real_main(argv)))
  File "/usr/lib/python3.10/site-packages/yt_dlp/__init__.py", line 930, in _real_main
    with YoutubeDL(ydl_opts) as ydl:
  File "/usr/lib/python3.10/site-packages/yt_dlp/YoutubeDL.py", line 914, in __exit__
    self.cookiejar.save(ignore_discard=True, ignore_expires=True)
  File "/usr/lib/python3.10/site-packages/yt_dlp/utils.py", line 1622, in save
    with self.open(filename, write=True) as f:
  File "/usr/lib/python3.10/contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "/usr/lib/python3.10/site-packages/yt_dlp/utils.py", line 1577, in open
    with open(file, 'w' if write else 'r', encoding='utf-8') as f:
PermissionError: [Errno 13] Permission denied: 'cookies.txt'
```