LoginSignup
208

More than 5 years have passed since last update.

Python:処理ファイルをGUIから選択する方法

Last updated at Posted at 2018-04-14

この記事について

Pythonを使ったデータ処理プログラムを作成していた時に、本当に初心者の時は処理ファイルのパスをスクリプトに手書きしてました。
ただ、「このフォルダにファイルを置かないと処理できません」「ファイル名がこれが仕様です」なんて汎用性が低すぎて完全自分専用のプログラムになります。

なので、処理ファイルをGUIから選択する仕様にしたく少し昔に方法を調べました。
その調査結果を備忘録も兼ねて残します。

処理環境

・Windows7 32bit
・Python3.6.2
※元々Python2.7.5で作成したコードを最新であるPython3.6.2に変換しています。

サンプルコード

サンプルとして選択したファイルの絶対パスをメッセージボックスとして表示させるコードを紹介します。

[2019.03.10追記]
Anacondaをインストールした環境で実行したらエラーが出ました。
私だけであれば良いですが・・・。
一応、Anaconda下でPythonを利用している方をご注意下さい。

⇒と思ったら、pythonファイル名を「tkinter.py」にしてることが原因でした・・・。初歩的すぎ・・・。

# モジュールのインポート
import os, tkinter, tkinter.filedialog, tkinter.messagebox

# ファイル選択ダイアログの表示
root = tkinter.Tk()
root.withdraw()
fTyp = [("","*")]
iDir = os.path.abspath(os.path.dirname(__file__))
tkinter.messagebox.showinfo('○×プログラム','処理ファイルを選択してください!')
file = tkinter.filedialog.askopenfilename(filetypes = fTyp,initialdir = iDir)

# 処理ファイル名の出力
tkinter.messagebox.showinfo('○×プログラム',file)

 
上記コードをコピーして実行してみてください。
環境やファイルの置き場所に依存しないと思うので、おそらく動きます^^


<処理結果>
①スクリプトファイルを実行
image.png

②コマンド画面とメッセージボックスが表示
※コマンド画面が不要の方はスクリプトファイルの拡張子を「.pyw」にすると表示されなくなります。
image.png

③OKを押してファイルを選択
ダイアログの表示パスは、「iDir = 」でスクリプトファイルの置き場所と同じパスで設定しています。
image.png

④ファイルを選択して【開く】と、絶対パスがメッセージボックスに表示されます。
image.png


良い感じでしょう!?

Python初心者の方でも以下のコードをコピペするだけで、処理ファイルをGUIから選択させることができます。
私は魔法のコードとして、コピペして使うこともあります。
※ただし、Pythonのバージョンにご注意ください。

# モジュールのインポート
import os, tkinter, tkinter.filedialog, tkinter.messagebox

# ファイル選択ダイアログの表示
root = tkinter.Tk()
root.withdraw()
fTyp = [("","*")]
iDir = os.path.abspath(os.path.dirname(__file__))
tkinter.messagebox.showinfo('○×プログラム','処理ファイルを選択してください!')
file = tkinter.filedialog.askopenfilename(filetypes = fTyp,initialdir = iDir)

おまけ情報

①選択ファイルを特定の拡張子だけに絞る
例)ファイル選択時にCSVファイルのみ表示させる。

# モジュールのインポート
import os, tkinter, tkinter.filedialog, tkinter.messagebox

# ファイル選択ダイアログの表示
root = tkinter.Tk()
root.withdraw()

# ここの1行を変更 fTyp = [("","*")] → fTyp = [("","*.csv")]
fTyp = [("","*.csv")]

iDir = os.path.abspath(os.path.dirname(__file__))
tkinter.messagebox.showinfo('○×プログラム','処理ファイルを選択してください!')
file = tkinter.filedialog.askopenfilename(filetypes = fTyp,initialdir = iDir)

# 処理ファイル名の出力
tkinter.messagebox.showinfo('○×プログラム',file)

image.png

 
②複数ファイルを選択したい場合
例)複数選択したファイルの絶対パスリスト作成

# モジュールのインポート
import os, tkinter, tkinter.filedialog, tkinter.messagebox

# ファイル選択ダイアログの表示
root = tkinter.Tk()
root.withdraw()
fTyp = [("","*")]
iDir = os.path.abspath(os.path.dirname(__file__))
tkinter.messagebox.showinfo('○×プログラム','処理ファイルを選択してください!')

# ここの1行を変更 askopenfilename → askopenfilenames
file = tkinter.filedialog.askopenfilenames(filetypes = fTyp,initialdir = iDir)

# 選択ファイルリスト作成
list = list(file)
tkinter.messagebox.showinfo('○×プログラム',list)

image.png

※選択したファイル群はタプルとして格納されます。
※リストにしたい場合ば以下の1行でリスト化しています。

list = list(file)

 
③ディレクトリを選択したい場合
例)選択したディレクトリのパスを表示

# モジュールのインポート
import os, tkinter, tkinter.filedialog, tkinter.messagebox

# フォルダ選択ダイアログの表示
root = tkinter.Tk()
root.withdraw()
fTyp = [("","*")]
iDir = os.path.abspath(os.path.dirname(__file__))
tkinter.messagebox.showinfo('○×プログラム','対象ディレクトリを選択してください!')

# ここの1行を変更
dir = tkinter.filedialog.askdirectory(initialdir = iDir)


# 処理ディレクトリパスの出力
tkinter.messagebox.showinfo('○×プログラム',dir)

image.png

 
④Python2.xとの相違
Python2.7.5のコードをPytho3.6.2に置き換えた際に変更した点はモジュール名のみになります。
<モジュール名の変更>
・ Tkinter → tkinter
・ tkFileDialog → tkinter.filedialog
・ tkMessageBox → tkinter.messagebox

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
208