python open windows explorer - python

Please image such a situation: A local file's icon is displayed in a GUI, right click the icon, a context menu pops, with the option: show file in explorer. Click the option, then a explorer window opened, with the particular file selected. Many editors have such a feature: show in folder or show in explorer
In fact, the GUI is built by PyQt, my first thought is simple, just open a subprocess and pass the command line:
explorer /select, a_full_path_name
The behavior is indeed what I need, but when click "show in folder" again, a new explorer window will be opened, even the old one exsits! How about a naughty boy clicking "show in folder" dozens of times in a breath? So I need just one window, if an old one exists, just raise it to the front.
The command start /D a_path . may disappoint the naughty boy(run it several times, only one window.) however, there is no option to highlight a selected file, thus also disappoint me...
As mentioned above, many editors have such a "show in folder" feature,
but to my suprise, PyCharm "Show in Explorer" will open multiple windows with multiple clicks on the same file, and also the CodeBlocks "opening containing folder", however programmer's notepad "open containing folder" will always open just one folder on the same file.(To be honest, I have only the 3 editors in my PC except the windows notepad :)
My Question:
Can the feature mentioned above be achieved just by windows cmd?
If can not, Is there a python way to achieve that?
In fact, I found several related questions in stackoverflow, for example,
but my problem is unsolved, would somebody give me a ride?

Finally, some nice guy guided me to the answer.
It's from https://github.com/exaile/exaile/blob/master/xl/common.py#L350
in py3+
import ctypes
ctypes.windll.ole32.CoInitialize(None)
upath = r"C:\Windows"
pidl = ctypes.windll.shell32.ILCreateFromPathW(upath)
ctypes.windll.shell32.SHOpenFolderAndSelectItems(pidl, 0, None, 0)
ctypes.windll.shell32.ILFree(pidl)
ctypes.windll.ole32.CoUninitialize()
in py2+
Just give a unicode path.
note: ILCreateFromPathW (Unicode) and ILCreateFromPathA (ANSI)

Related

PyCharm, open new window for each "View as DataFrame"

In Debug mode, I would like PyCharm to open a new window (not a new tab!) for every "View as DataFrame" click I do.
Right now, they are opened as tabs, which makes it very difficult to compare.
Other posts suggest to klick the "Open in editor" button to then move it. This works but it is annoyingly cumbersome. And I remember in the past multiple windows opened (maybe in the community edition?)

Python IDLE Unable to Be Opened

A little context
I'm using Windows. I installed Python and wrote a script for it. Before long, it became necessary to install packages. To do so, I needed to know what was the location of my Python folder, so that I could enter this location in the command prompt as in this video at 7:55. My idea for how to achieve this was to go into the "This PC" app (as in, enter the words "This PC" into the search bar which is next to the start symbol in the lower left-hand (LLH) corner of the screen) and search within that app the phrase "Python" (as in, enter "Python" into the search bar which is in the URH corner of the window which is opened when the symbol "This PC" is clicked). Then, I just made a new folder titled 'Python 3.7' on my desktop and dragged all of the search results into that new folder.
Slightly more detailed explanation of the problem
Now, everything can be found in that folder, and I find it to be reasonably organized, but most things no longer function if I attempt to click on them. For example, clicking on the 'IDLE (Python 3.7 32-bit)' shortcut has no effect whatsoever. The shortcut which I have pinned to my taskbar also inexplicably no longer does anything when it is clicked. Going to the .py script I wrote, right clicking, and selecting "Open" has no effect; on that same menu, selecting "edit with IDLE" opens a window asking "How do you want to open this .py file?" I select the option "Python" and click OK, and nothing happens.
What I expect to be the most relevant information
The thing titled 'IDLE (Python 3.7 32-bit)' which I click on is categorized as a Shortcut. When I right-click and go into the properties, it says the Target Location is
Python37-32
which is a folder. It further says Target:
\Programs\Python\Python37-32\Lib\idlelib\idle.pyw"
and Start in:
"C:\Users\Myname\Desktop\Python 3.7\python\Python37-32\"
whereas the current location of the Shortcut is
C:\Users\Myname\Desktop\Python 3.7
Essentially, I have rearranged folders without much thought, and inadvertently messed something up?
Does someone happen to know, at least, what this issue is - or, perhaps, to be so gracious as to refer me to a help page on the subject - so that I may research the issue?

Copy/Paste from Python command line to sublime

Apologies in advance for what should seem obvious to me...
New to Python, but other wise very experience, hence the frustration.
As I'm learning, I'm attempting to prototype the command at the command prompt (">>>") as I script. I can enter the python commands and validate they are correct, but (currently) then have to retype the command in to sublime, which seems inane, especially as I'm watching videos where they are copying and pasting one to the other...
Windows 7 CMD, & Python ">>>" command lines, Sublime window
I'm trying to copy commands from the Python command line ">>>" to the sublime editor in an open/new tab. Normally, I would mark/highlight the selection, then Ctrl+c, place the cursor in the target window (sublime in this case), Ctrl-v, and be done.
However, I'm unable to select what I want to copy (??). Up/down arrows will recall the individual lines (no problem with that) -- where I would usually do a Ctrl-(left/right arrow) to select the text, but no indication that anything has been selected. Same result using the mouse.
Thanks in advance for the assist.
This is an oddity of the Windows command prompt. First, there is a little bit of setup. Then you can do the cut-and-paste in a slightly different way than you are used to.
Setup
Right click on the title bar at the top. Select "Properties". Click on the "Options" tab. Make sure that the "QuickEditMode" box is checked. Close the Properties window.
Cut-and-paste should be enabled, but not using control-C to cut. Select the text you want to copy. Instead of typing control-C, just hit return (with the desired text selected). The text should now be on the clipboard to paste into other applications (with control-V)
In Linux terminals, pressing Ctrl+k copies everything in the line that follows the cursor. Ctrl+u copies the everything before the cursor.

Change Windows File Handler to a Python script to open a file [duplicate]

I want to do the following:
Save numeric data in a CSV-like formatting, with a ".foo" extension;
Associate the ".foo" file extension with some python script, which in turns opens the .foo file, reads its content, and plots something with a plotting library (matplotlib most probably).
The use-case would be: double-click the file, and its respective plot pops up right away.
I wonder how I should write a python script in order to do that.
Besides, the windows "open with" dialog only allows me to choose executables (*.exe). If I choose "fooOpener.py", it doesn't work.
This isn't really a programming question, but what you need to do is to figure out how to get the Python executable into the registry key that opens your data file.
For example, I created a little Python script called opener.py that looks like this:
import sys
print(sys.argv)
input()
Then I created a testfile.foo and used the "change" button in that file's property dialog to choose opener.py. (You can do this if you click Browse and change the Open With dialog's file filter to "All Files".)
Of course this didn't work (as you noticed). So I opened regedit and searched for opener.py and found it at the following registry key:
HKEY_CURRENT_USER\Software\Classes\Applications\opener.py\shell\open\command
The default value of this key was "C:\opener.py" %1. I changed it to python "C:\opener.py" %1. It worked!
Long story short, to do this properly you need to custom-edit the registry. Actually setting up the file association is more complex than just editing that one key (you have to also indicate that .foo is associated with opener.py).
An alternative approach would be to turn your Python script into a standalone executable using one of the several tools available for that purpose, or write a small executable in C that launches the script.
press the windows key
type cmd
right click the result and choose "run as administrator"
assoc .foo=foofile
ftype foofile="C:\Users\<user>\AppData\Local\Programs\Python\PYTHON~1\python.exe" "C:\<whatever>\fooOpener.py" "%1" %*
Use pythonw.exe if it's a .pyw file (to prevent a cmd window from spawning).
If you want to use an existing file type, you can find its alias by not assigning anything. For example, assoc .txt returns .txt=txtfile.
instead of editing registry, you can create batch file
opener.cmd
with content
python "C:\<whatever>\fooOpener.py" %1
and associate extension with this file.
It works!

How to clear console in sublime text editor

How to clear console in sublime text editor.
I have searched on internet too..But can't find proper shortcut for that.
Please provide info
I assume you're talking about the console accessible via View -> Show Console or Ctrl`. Unlike other answers on SO, which deal with clearing the Python console when opened from the command prompt, os.system("cls") or os.system("clear") (depending on your OS) don't work with the Sublime console. The Sublime API (version 2 or version 3) does not have any built-in console-clearing method, and I was unable to find any undocumented method in sublime.py or sublime_plugin.py. The console appears to be read-only, as selecting all the text and hitting Delete doesn't work either.
I've been looking into this for some time, and I've come to the conclusion that it does not appear to be possible. However, there is a hackish workaround - just run print('\n'*100) to print 100 newline characters, and you won't be able to see any of the previous output unless you scroll up some distance.
I installed ClearConsole package, then type alt+k to clear then console.
A new window will always have a clean console. You can use this to open a new window, close the old one and then reopen your project (assuming it is a saved project and not anonymous). This requires the hot_exit setting to be true, which is the default.

Categories

Resources