python利用文件对话框获取文件路径

一、单文件

python3:

1 import tkinter as tk
2 from tkinter import filedialog
3 
4 root = tk.Tk()
5 root.withdraw()
6 
7 file_path = filedialog.askopenfilename()

 

python2:

1 import Tkinter, tkFileDialog
2 
3 root = Tkinter.Tk()
4 root.withdraw()
5 
6 file_path = tkFileDialog.askopenfilename()

 

二、多文件

 1 import tkinter as tk
 2 from tkinter import filedialog
 3 
 4 root = tk.Tk()
 5 root.withdraw()
 6 
 7 file_path = filedialog.askopenfilenames()
 8 for f in file_path:
 9     fo = f.split('.')[0]+'.csv'
10     with open(fo,'w') as foo:
11         with open(f,'r') as fn:
12             fn.readline()
13             for line in fn.readlines():
14                 li = line.strip().split()
15                 foo.write('%f,%f\n'%(float(li[1]),float(li[0])))
16                 print(li)

 

posted @ 2018-04-10 15:41  絳紫色  阅读(2216)  评论(0编辑  收藏  举报