How to access Linux's downloads folder using python?

1k views Asked by At

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.

2

There are 2 answers

2
CoderCharmander On BEST ANSWER

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

0
Corralien On

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.