How can I get a file or directory name from a file open dialog in PyQt4? - pyqt4

I am writing a PyQt4 application and one of the file types I wish to open is an Esri Grid format which, rather unusually, is a directory. I also wish to open other GIS filetypes that are just files (e.g. geotiffs). I can open these filetypes OK with the GDAL library by passing either a file or directory name and GDAL figures it out.
The problem I have is making the GUI. I want to open a file open dialog and get either a file name, or directory name. The problem is that the file dialog won't let me choose a directory - only files. I need the dialog to return a path to either. I've tried it on Mac and Linux.

I know PySide has a method called getExistingDirectory
http://pyside.github.io/docs/pyside/PySide/QtGui/QFileDialog.html
PyQt is basically the same, so it should have a similar method. http://pyqt.sourceforge.net/Docs/PyQt4/qfiledialog.html It is in the static methods section.

I think I've cracked it. This snippet tests the functionality I need:
dlg=QtGui.QFileDialog()
dlg.setFileMode(QtGui.QFileDialog.AnyFile);
e=dlg.exec_()
print dlg.selectedFiles()[0]
The solution was to set the file mode to 'AnyFile'. This allows the file dialog to return both directory and file names.

Related

How does File Explorer open files?

It's maybe stupid question, but what happens behind the scenes when i double click the word app, or .exe app?
For 'Non-Developer' it just opens the file in right environment after doubleclicking. But I would like to know, how is it done, how can the file manager know what to open? (.docx in word, .txt in texteditor,etc...)
+ I would like to know how can I do that in Node.js, is it the best way to use child_process and if statements for every suffixes?
There is a file association to tell Windows how to treat a certain file extension. You can type assoc in a Windows Command Prompt to see them. As an example:
C:\test>assoc .txt
.txt=txtfile
So Windows knows now, that the file with the extension .txt is a txtfile.
ftype defines, how that filetype is to be handled:
C:\test>ftype txtfile
txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1
So whenever you doubleclick on a file, Windows checks if it is associated with any filetype. Then it looks up, how to handle that filetype and executes that command (in the example above, it opens Notepad with the filename as a parameter).
Both assoc and ftype are able to change the settings (for example to open .txt files with another editor). But if you try that, do yourself a favor and note the original settings, so you are able to revert your changes when needed.
The changes can also be done directly in the registry (not recommended, when you are not experienced in handling the registry)
For Node.js, you can use child_process to shell out to the start command which does similar things as the File Explorer.
E.g. start some/path/to/file.docx will open that file in the default program associated with the format.

Nodejs: Detect file type without extension

I'm looking to remotely download and detect a file from a website, like this
http://examplewebsite.com/100/download
When viewing in my browser, this automatically downloads as the appropriate file type, 100.pdf, but sometimes it can be a .xls or .doc file. etc.
Looking at libraries available, like file-type, it only works if you already have the extension
Is this possible?
If you have the url, you can split by '.' and select the last element of your list.
The file-type library you linked in your question actually checks the source of the file to guess. It doesn't use the file extension at all.

How to avoid .pyc file listing in open File Dialog sublime text

i am trying to find if any possible configuration is available in sublime-text editor that can ignore the listing of *.pyc files in OPEN FILE DIALOG because,
its problematic and slow when Press Ctrl+O and type file name and its select file with *.pyc format. and we require to select next file to to open actual *.py file.
I tried file names in ascending order in Open file Dialog that lists *.py files first & *.pyc second. but i work with files mostly starts with a,x,y,w and that's why i require solution.
Sublime uses the underlying Open dialog of whatever operating system you're running it on, which means that it's technically up to the operating system in general to provide the capability to provide that kind of filtering.
If you're using Windows, you can change the file filter to Python, which will exclude *.pyc files. You can also enter *.py in the file name box and hit enter to get the dialog to show you only files with that extension.
On MacOS you can enter *.py in the search box in the top right of the open dialog to find files of that type. As far as I'm aware that's always going to search your whole Mac for files of that type and not just the folder that you're currently looking in, though (I don't use this particular feature of MacOS).
On Linux, well, that's going to depend on your Linux distribution in general. On my particular Linux machine, the dialog has a Search button that works similar to the one on MacOS, finding all files everywhere that match instead of just filtering the current location. Other distributions may have something similar to this or Windows.

QFileDialog choose file or folder

Is there a way to choose file or a folder in QFileDialog window. As far as I've reviewed the documentation and it turns out that I can either open file or directory but I can't use the dialog to open both types at the same time. Is there a way I can do this?

Coredata: opening an xcdatamodeld file

I have a single xcdatamodeld file and would merely like to open this particular file with xcode. However, when I double click the file, it doesn't really show me anything or allow editing. The only way to do this is to run MyApp.xcodeproj and only then can I modify this file. Isn't there any way to edit the coredata file using only the xcdatamodel file? Thanks!
The xcdatamodeld file is not actually a file - it's a directory containing other model files.
Right click the file, choose Show Package Contents and you will get access to the files inside the directory. You can then double-click those files. When I do so I get just the Xcode model editor without the full Xcode project window.

Resources