我有一个python脚本,它读取文件并将其内容复制到另一个文件,同时在发送之前删除不需要的行。
问题是我想允许用户选择源文件和目标路径。
如何解决这个问题?
#!/usr/bin/python
'''FUNCTION THAT READ SELECTE DFILE AND WRITE ITS
CONTENT TO SECOND FILE WITH DELETING
TH EUNWANTED WORDS'''
import Tkinter
from os import listdir
from os.path import isfile
from os.path import join
import tkFileDialog
import os
def readWrite():
unwanted = ['thumbnails', 'tyroi', 'cache', 'Total files', 'zeryit', 'Ringtones', 'iconRecv',
'tubemate', 'ueventd', 'fstab', 'default', 'lpm']
mypath = r"C:\Users\hHJE\Desktop/filesys"
Tkinter.Tk().withdraw()
in_path = tkFileDialog.askopenfile(initialdir = mypath, filetypes=[('text files', ' TXT ')])
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for file in files:
if file.split('.')[1] == 'txt':
outputFileName = 'Sorted-' + file
with open(mypath + outputFileName, 'w') as w:
with open(mypath + '/' + file) as f:
for l in f:
if not True in [item in l for item in unwanted]:
w.write(l)
print ("
*********************************\
THE OUTPUT FILE IS READY\
*********************************\
")
in_path.close()
if __name__== "__main__":
readWrite()
答案 0 :(得分:1)
您可以像使用TkFileDialog
一样询问inputFiles:
outputpath = tkFileDialog.asksaveasfile()
请参阅这些教程中的示例:http://www.tkdocs.com/tutorial/windows.html