1

I have a python script that reads a file and copies its content to another file while deleting unwanted lines before sending.

The problem is that I want to allow the user to choose the source file and the destination path.

How can this be solved ?

outputSortedFiles.py

#!/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()

2 Answers 2

1

You can use TkFileDialog just as you did to ask inputFiles :

outputpath = tkFileDialog.asksaveasfile()

See examples in those tutorials : http://www.tkdocs.com/tutorial/windows.html

Sign up to request clarification or add additional context in comments.

2 Comments

this isn't what i want i want to allow user to select the source file and select the output path ... i do not know if you did understand my question
In fact i don't get your need, maybe elaborate a bit more ?
0

If you simply want the user to choose the directory:

from tkinter import filedialog
outputpath = filedialog.askdirectory()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.