I have a program that opens a lot of folders right now, and I am hoping that I can use the already open explorer instances to open new ones. I have this code:
import subprocess
subprocess.Popen(r'explorer /select,"C:\path\of\folder\file"')
I have seen threads on doing this in C#. Does anyone know a way it can be done in Python?
Thanks,
Ben
I do not believe you can do this. If there is any hope you will probably need to look at Python for .Net, you might give IronPython a try. But the code above using subprocess is simply giving you the same result as if you ran that string in command line. Which launches the explorer application with those arguments. But once its started, its off running on its own. Its not looking for command line input any more. I've looked at the list of command line arguments for explorer, and I see nothing that would indicate you can manipulate it once its running. Sorry, hope this helps.
Related
I wrote a python script to run some simple tasks and this issue has been happening randomly at times.
Upon clicking on .py file, a cmd prompt window appears for a some microseconds and closes immediately without showing any text.
I thought this issue was because the code finished running too fast at first but the code doesnt actually run. I know this because the code involves sending a text message on discord through the requests module and I can see post-running that no text has been sent.
If it was the prior assumed issue, I would've just added some input for the program to recieve but my program has an infinite while loop in it which should be already enough to keep the cmd window open. Hence I dont understand what's causing this.
The last time it happened I somehow found a solution which I followed step by step and was able to resolve it but its happening again now and I cant find that solution again unfortunately. From vague memory, I recall the solution involved running some commands on windows terminal.
Are you double clicking the .py file? If so, it may not work in some situations. What you should do is open up your cmd and type the following.
C:\Users\Admin> python filename.py
You can replace filename with your file's name.
If you are using Mac or any Apple devices, you will need to replace python with python3.
I have just written a bunch of lines of code on the Python prompt at the terminal. Now, I want to save all those lines of code to a .py file.
I am unable to find out how to do that. The only thing that I could find on StackOverflow was this answer but it shows only how to do it in an iPython notebook. I am not using an iPython notebook. I am running the code at the command line on the terminal.
I tried to follow that answer (because just in case) and ran the %save magic command on the terminal but it gave a SyntaxError.
So, how do save it?
Thanks!
See http://blog.e-shell.org/174 . As wu explains, the python prompt is using readline, and you can import a Python library to access this.
>>> import readline
>>> readline.write_history_file('/path/to/history.txt')
You can trying using another interpreter : bpython , I belive it has what you need,check it out.
Save the code you've entered to a file.
You seem to be affected by the misconception, that the python environment is workspace-centered (similar to what I know from Smalltalk and some LISP variants):
fire up with an initial workspace
modify by your liking
store the result
This is unfortunately not the case. While you can import existing files, the other option is to specify an existing file as initially to be loaded and keep the interpreter open by using the -i option.
It really depends on your terminal for the exact commands.
The general idea is to copy everything (if possible) or one page at a time from the terminal into a text editor and then clean the >>> prompts (and possibly other formatting problems) in the text editor.
But anyway, typing a lot of commands directly in the execution environment if really bad practice. At least you test a handful of lines and immediately save them in a file. IDLE is great at this game...
I want to run a python script as a process/in the background. I searched and found pythonw.exe but when I do pythonw.exe name_of_my_script.py nothing happened. Am I doing something wrong and/or how else would I run it in the background?
This is part of my code that do problem :
from selenium import webdriver
I will thank you for any help whatsoever.
note:
With the help of the comments here, I understood that also python.exe running don't work and bring error - NameError: name 'PROTOCOL_TLS' is not defined ( even tough in normal running it doesn't have errors.
edit:
i used http://pytoexe.com/ and i got exe file that work but the cmd console still exist ( mabye it because i use phantomjs ? ) click here
You can create your own exe by going to http://pytoexe.com .
After that convert your script. Choose windows-based and convert it.
You'll not have any problems after that. Hope that helps.
I'd approach this by using threading module, and use os.fork() - but I'm not running Windows. I suggest having a look through Difference in behavior between os.fork and multiprocessing.Process for other OSes.
By "I want to run a python script as a process/in the background," do you mean make the window not visible? if so, just save the script as .pyw instead of .py
So I'm trying to setup the terminal in conjunction with Notepad++ and I'm a little confused. I've managed to download Python 2.7 and load it up in Powershell but I don't really know where to go from there. Do I even need to use a terminal? I know it says you can't use IDLE but it would be a lot easier. If anyone can guide me through getting the right setup to go on with the book I'd appreciate it a lot.
If you managed to get python running in the powershell following the Exercice 0, and you read the "Warning For Beginners" section, you can proceed to Exercice 1.
Why do they recommend that setup? Notepad++ is on Windows a powerful text editor very similar to other text editors, so you will soon get familiar with it. You will need to save your scripts in files and type a special line in the powershell to run and test them. That way you will get accustomed to running programs with arguments and if you make a failure, you can edit your code, save it and run it again.
In IDLE, you are already in a Python environment, so you cannot give arguments to your program inside it (actually you can but it's complex). You can also get an editor, save your file and run the script but also here it's a harder and less obvious way than Notepad++. When you get comfortable with the environment in that book you may change to get something that suits better your way of doing.
I've found a lot of people having the reverse issue, but haven't yet found a question that involves IDLE not being able to run something that runs fine from the command line. I'm using a new module that I haven't used before that uses one .pyd file and one .dll, and involves a device that connects through USB. I sadly can't post in-depth code snippets since this is copyrighted code, but if anyone knows where to start on a problem like this I would be very grateful.
IDLE swaps out the sys.stdout and sys.stderr objects at the Python level this causes issues with some pyd modules. Try using another debugger.