1

Background

I am working on a basic text editor, and need to use Windows Explorer to get the path of a file. This is my code currently, but it simply opens the file - I need it to return the path as a string:

import subprocess
subprocess.Popen(r'explorer /select, "C:\"')

Question(s)

  1. How would I have it return the path as a string?
  2. How would I use the path to access a specific file? For instance, if I wanted to open file myFile, but it wasn't in the same folder as my program, how would I have it access that file, in a different folder? Sorry for the ambiguity!

Tech Specs

OS: Windows 7
Language: Python 2.7.3

psubsee2003
  • 8,563
  • 8
  • 61
  • 79
xxmbabanexx
  • 8,256
  • 16
  • 40
  • 60
  • 1
    Do you want to display a window for the user to select a file? If so look at http://www.daniweb.com/software-development/python/threads/39327/file-dialog-window-save-dialog-window – Preet Sangha Feb 15 '13 at 02:04
  • @PreetSangha Thanks for the suggestion, but I don't need that. I am using [this](http://stackoverflow.com/questions/281888/open-explorer-on-a-file/281911#281911) for that (it is essentially the code that I posted here.) I need the script to not open the file, but to return the path, and then despawn. Sorry if there was ambiguity. – xxmbabanexx Feb 15 '13 at 02:14
  • 3
    @xxmbabanexx I'm not sure what you're trying to do for your bonus, and your original question is *slightly* vague too, but would `tkFileDialog.askopenfilename(initialdir='C:\\')` work for what you want? It looks like the native dialog mostly everything else uses (e.g. the same as when I CTRL+S in Chrome). – Nathan Feb 15 '13 at 02:35
  • @Nathan that was really helpful! How would I specify a specific file type? For instance, if I make my files save as `.myTXT` how would I make it so that `Windows Explorer` could only open files of that type? – xxmbabanexx Feb 15 '13 at 02:53
  • @Nathan can you post that as an answer, so that I can give you the rep? It was really helpful :) – xxmbabanexx Feb 15 '13 at 12:25
  • Use a file picker dialog for this. – David Heffernan Sep 06 '15 at 08:51

2 Answers2

1

I would not recommend using Windows Explorer for this purpose, you might want to look at Tkinter. This is very close to this other question.

The main reason for using a third party library is that python runs on multiple platforms. Choosing a file on OSX and Windows 7 and Ubuntu is of course pretty different. This is the main reason why it is not part of the python runtime.

Community
  • 1
  • 1
Eric
  • 19,525
  • 19
  • 84
  • 147
  • `Tkinter` is in stdlib that works on Windows, OSX, Ubuntu. It should be available by default if `python` is installed in a GUI environment. Though if OP wants a "native" look then something like PySide could be used instead. – jfs Sep 06 '15 at 12:47
0

About question 2, to open a file located in the process working directory, you could use:

file = open('filename.txt', 'r')

To open a file which is in a different directory, you can use:

directory = 'C:\Users\MyName\Documents\example.txt'
file = open(directory, 'r')

That would also work, opening the file at the directory specified. If there is no such file in the directory, you would get the following error:

 File "filename.py", line #, in <module>
   file = open('filename.txt', 'r')
 IOError: [Errno 2] No such file or directory: 'filename.txt'
notTypecast
  • 460
  • 5
  • 16