views:

57

answers:

1

Dear All,

I have written some encryption code in python that takes raw input message from user and then encrypts and decrypts it using AES. Now i want to enhance the working and i want that i can open the windows explorer from my code and browse to any file on my computer, select it and when i press OK button the path to file is stored in a variable so i can use it for processing.

I have search many forums, i have managed to open windows explorer but there is no traditional OK and Cancel button. If user presses OK button the path to the file should be stored in my code variable.

Any help in this regard will be highly appreciated.

moreover, just to let you know i have used the following code:

import os
os.system("start .")

but the explorer window doesnt have any cancel or OK button. Please help

+1  A: 

That is because what you see when you open files in Windows is not actually an Explorer window, it is called a common dialog. I am assuming you are referering to this dialog:

Open file dialog

There are different ways you can go about opening the common open dialog, among the most easiest is probably just using the Tkinter module from the Python standard library, namely the tkFileDialog module's askopenfilename.

Example code:

import Tkinter
import tkFileDialog

root = Tkinter.Tk()
root.withdraw()

filename = tkFileDialog.askopenfilename(parent=root,title='Open file to encrypt')

As for the curly braces: You are using askopenfilenames to tell Tk that you possibly want more than one filename. That's why you get a list of filenames enclosed in curly braces. I actually suspect an oversight in Python's Tk binding so that the filenames are not split up and a list is returned, but this is easily remedied using code similar to this:

import re
# ...
# ...
filenames = tkFileDialog.askopenfilenames(parent=root)
files_to_process = re.split("\}\W\{", filenames[1:-1])

This will give you a list of the selected filenames in case the user selects more than one file. It will break when only passed a single filename, so be sure to check for that case.

Jim Brissom
ah....thats exactly what i am looking for. Unfortunately i am a beginner to python so dont know much how to use it. what i assume is that i have to import tkFileDialogand how to use askopenfilename... If you can provide one line syntax for my understanding , that will be great.
fahad
i have used the code below, search it over the internet.from tkFileDialog import askopenfilenamesa = askopenfilenames()print athis is solving my problem but the only thing is that it is giving the output in curly brackets, how to remove those curly brakets or read path without them? Also a small window appears which is blank and doesnt do anything? how to avoid opening it, it automatically opens too. any idea why is that?{C:/Users/miracle/Desktop/testing python file read/testit.txt}
fahad
from tkFileDialog import askopenfilenames file_name = askopenfilenames() print file_name myfile = open(file_name) The following error occurs. As far as i can see it is because of the curly brackets and may be we need to add special mode to read the file , provided by the path selected. IOError: [Errno 22] invalid mode ('r') or filename: u'{C:/Users/miracle/Desktop/testing python file read/fahad.txt}' Please guide!
fahad
The window you see is the Tk root window, which you can easily withdraw in this case, thus hiding it. I added a code snippet to my answer above.
Jim Brissom