Home > Software design >  How to access Linux's downloads folder using python?
How to access Linux's downloads folder using python?

Time:12-27

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.

CodePudding user response:

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'], capture_output=True)
download_path = process.stdout.strip().decode()
print(download_path)

# Output:
/home/corralien/Downloads

CodePudding user response:

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

  • Related