0
0

More than 1 year has passed since last update.

Windowsのファイルの保存場所を開く(Open File Location)コマンドをPythonで実行する

Last updated at Posted at 2022-09-25

実現目標

・スタートメニューから More > Open File Location
・ショートカットから 右クリック > Open File Location

「対象のファイルが存在するフォルダが開き(または、移動して)、対象のファイルがアクティブになる」操作をPythonで実行する

用途

たとえば、Imageビューアなどで右クリックして画像の保存場所を開けます。

コード

import os
import subprocess

def open_file_location(absolute_path):
    # windows用のパスへ変換
    path = os.path.realpath(absolute_path)
    # subprocessでコマンドシェルを実行
    subprocess.Popen(f'explorer.exe /select, {path}')

open_file_location("C:/Images/image1.jpg")

ポイント

subprocess.Popenで/selectを指定することで、ファイルがアクティブ(ファイル選択状態)になるところがポイントです。あと、,をお忘れなく。

参考

0
0
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
0
0