• Uncategorized

About python : How-to-access-Linuxs-downloads-folder-using-python

Question Detail

In ubuntu the downloads folder is located in home\ubuntu\Downloads, but I don’t know if different distros have the same “style” (eg. home\arch\Downloads). Is there a “universal path” for all distros?
For anyone wondering i need to make a new directory in downloads.

Question Answer

On Linux, you can use xdg-user-dir from freedesktop.org project. It should work on every recent Desktop environments (KDE, Gnome, etc) and all recent distributions:

import shutil
import subprocess

xdg_bin = shutil.which('xdg-user-dir')
process = subprocess.run([xdg_bin, 'DOWNLOAD'], stdout=subprocess.PIPE)
download_path = process.stdout.strip().decode()
print(download_path)

# Output:
/home/corralien/Downloads

If you have Python 3.7 or higher, you can use the capture_output=True argument instead of the stdout argument.

Your “home directory” (functionally similar to C:\Users\YOUR_USERNAME on Windows) is at /home/YOUR_USERNAME on most Linux distros, and this is where the Downloads folder is located usually. The way to be the most sure about getting the correct directory is by using pathlib.Path.home():

from pathlib import Path
downloads_path = str(Path.home() / "Downloads")

Taken from this answer

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.