LoginSignup
6
11

More than 3 years have passed since last update.

Pythonでデスクトップのパスを得る

Posted at

PythonでmacOSとWindows共通でデスクトップのパスを得る方法。

いろいろありますが、os.path.expanduserを使うのが簡単です。

get_desktop.py
import os
desktop_dir = os.path.expanduser('~/Desktop')
print(desktop_dir)

環境変数から取得するのも良い方法ですが、その場合、OS判定が必要です。

get_desktop2.py
import os
if os.name == 'nt':
    home = os.getenv('USERPROFILE')
else:
    home = os.getenv('HOME')
desktop_dir = os.path.join(home, 'Desktop')
print(desktop_dir)

以上、簡単な紹介でした。

6
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
11