Windows platform: Consider using %APPDATA% or %LOCALAPPDATA% for application-generated files #22925

Open
opened 2026-02-21 11:13:34 -05:00 by deekerman · 6 comments
Owner

Originally created by @denizoezmen on GitHub (Feb 14, 2021).

Checklist

  • I'm reporting a feature request
  • I've verified that I'm running youtube-dl version 2021.02.10
  • I've searched the bugtracker for similar feature requests including closed ones

Description

When downloading files from certain URLs on the Windows platform, youtube-dl (or one of its libraries?) will generate a ".cache" directory directly in the root of the user's profile directory (e.g. C:\Users\<username>\.cache).

Example:

youtube-dl --no-check-certificate https://soundcloud.com/laurence_chapman/heavens-vault

will create a file called C:\Users\<username>\.cache\youtube-dl\soundcloud\client_id.json

Unlike on *nix platforms, file and directory names starting with a dot are not hidden by default on the Windows platform. Using multiple applications that exhibit this behaviour thus clutters up the user's profile directory.

On Windows, application-specific data should usually be placed in the profile directory's AppData\Roaming subdirectory (%APPDATA%) for "permanent" data that would also be considered in a roaming profile, or the AppData\Local subdirectory (%LOCALAPPDATA%) for mere temporary files.

Please consider using these directories instead (if feasible).

Originally created by @denizoezmen on GitHub (Feb 14, 2021). ## Checklist - [X] I'm reporting a feature request - [X] I've verified that I'm running youtube-dl version **2021.02.10** - [X] I've searched the bugtracker for similar feature requests including closed ones ## Description When downloading files from certain URLs on the Windows platform, youtube-dl (or one of its libraries?) will generate a ".cache" directory directly in the root of the user's profile directory (e.g. `C:\Users\<username>\.cache`). Example: `youtube-dl --no-check-certificate https://soundcloud.com/laurence_chapman/heavens-vault` will create a file called `C:\Users\<username>\.cache\youtube-dl\soundcloud\client_id.json` Unlike on *nix platforms, file and directory names starting with a dot are not hidden by default on the Windows platform. Using multiple applications that exhibit this behaviour thus clutters up the user's profile directory. On Windows, application-specific data should usually be placed in the profile directory's _AppData\Roaming_ subdirectory (%APPDATA%) for "permanent" data that would also be considered in a roaming profile, or the _AppData\Local_ subdirectory (%LOCALAPPDATA%) for mere temporary files. Please consider using these directories instead (if feasible).
Author
Owner

@rautamiekka commented on GitHub (Feb 14, 2021):

It's more than feasible, gallery-dl has done that by default (other places're used in order too).

@rautamiekka commented on GitHub (Feb 14, 2021): It's more than feasible, gallery-dl has done that by default (other places're used in order too).
Author
Owner

@pukkandan commented on GitHub (Jun 6, 2021):

User can change it using --cache-dir

@pukkandan commented on GitHub (Jun 6, 2021): User can change it using `--cache-dir`
Author
Owner

@denizoezmen commented on GitHub (Aug 29, 2021):

Thank you for the suggestion. However, I wouldn't classify this as a solution, since usage of the correct (platform-depent) directories intended for application data should be the default behaviour.

@denizoezmen commented on GitHub (Aug 29, 2021): Thank you for the suggestion. However, I wouldn't classify this as a solution, since usage of the correct (platform-depent) directories intended for application data should be the default behaviour.
Author
Owner

@dirkf commented on GitHub (Apr 21, 2022):

if params.get('cache_dir') is None and platform == 'Windows':
    params['cache_dir'] = ...
@dirkf commented on GitHub (Apr 21, 2022): ``` if params.get('cache_dir') is None and platform == 'Windows': params['cache_dir'] = ... ```
Author
Owner

@pukkandan commented on GitHub (Apr 21, 2022):

This is not the intended use of site.userbase and it may not always point to the appdata. Better to expand the relevant env vars

github.com/ytdl-org/youtube-dl@a0068bd6be/youtube_dl/options.py (L70)

That said, I don't know if this change is justified. When a user updates to a version with this change, the old .cache directory will remain, and even --rm-cache-dir will no longer remove it. Additional checks may need to be done to handle this

@pukkandan commented on GitHub (Apr 21, 2022): This is not the intended use of `site.userbase` and it may not always point to the appdata. Better to expand the relevant env vars https://github.com/ytdl-org/youtube-dl/blob/a0068bd6bec16008bda7a39caecccbf84881c603/youtube_dl/options.py#L70 That said, I don't know if this change is justified. When a user updates to a version with this change, the old `.cache` directory will remain, and even `--rm-cache-dir` will no longer remove it. Additional checks may need to be done to handle this
Author
Owner

@dirkf commented on GitHub (Apr 21, 2022):

OTOH compat_getenv('APPDATA') won't find ~/.cache.

I share the concern about the pain of moving the default cache to a new location. Perhaps the old default should be maintained if present?

So

--- old/youtube-dl/youtube_dl/cache.py
+++ new/youtube-dl/youtube_dl/cache.py
@@ -19,11 +19,26 @@
     def __init__(self, ydl):
         self._ydl = ydl
 
-    def _get_root_dir(self):
+    def _get_old_root_dir(self):
         res = self._ydl.params.get('cachedir')
         if res is None:
             cache_root = compat_getenv('XDG_CACHE_HOME', '~/.cache')
             res = os.path.join(cache_root, 'youtube-dl')
         return expand_path(res)
+
+    def _get_root_dir(self):
+        res = self._get_old_root_dir()
+        if os.path.exists(res):
+            return res
+        res = self._ydl.params.get('cachedir')
+        if res is None:
+            # LOCALAPPDATA "new" in Windows 7
+            res = compat_getenv('APPDATA')
+            if res is not None:
+                # assumption: setting APPDATA means this is the answer
+                return os.path.join(res, 'youtube-dl', 'cache')
+            res = compat_getenv('XDG_CACHE_HOME', '~/.cache')
+            res = os.path.join(res, 'youtube-dl')
+        return expand_path(res)
 
     def _get_cache_fn(self, section, key, dtype):
@dirkf commented on GitHub (Apr 21, 2022): OTOH `compat_getenv('APPDATA')` won't find `~/.cache`. I share the concern about the pain of moving the default cache to a new location. Perhaps the old default should be maintained if present? So ```py --- old/youtube-dl/youtube_dl/cache.py +++ new/youtube-dl/youtube_dl/cache.py @@ -19,11 +19,26 @@ def __init__(self, ydl): self._ydl = ydl - def _get_root_dir(self): + def _get_old_root_dir(self): res = self._ydl.params.get('cachedir') if res is None: cache_root = compat_getenv('XDG_CACHE_HOME', '~/.cache') res = os.path.join(cache_root, 'youtube-dl') return expand_path(res) + + def _get_root_dir(self): + res = self._get_old_root_dir() + if os.path.exists(res): + return res + res = self._ydl.params.get('cachedir') + if res is None: + # LOCALAPPDATA "new" in Windows 7 + res = compat_getenv('APPDATA') + if res is not None: + # assumption: setting APPDATA means this is the answer + return os.path.join(res, 'youtube-dl', 'cache') + res = compat_getenv('XDG_CACHE_HOME', '~/.cache') + res = os.path.join(res, 'youtube-dl') + return expand_path(res) def _get_cache_fn(self, section, key, dtype): ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/youtube-dl#22925
No description provided.