This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
DirectoryInfo.Delete(True) Doesn't Delete When Folder Structure is Open in Windows Explorer
Folks, I am writing a python testing harness and part of the project involves uninstalling an application and then reinstalling it every night. Part of the uninstall task involves deleting the data directory. During the day testers/developer will be logging into the computer and occasionally a command prompt and/or log file is left open. This causes my script to fail as windows will not let me delete a file if another process has a handle to it.
I would like to know if it is possible to get the pid of the process that is holding onto the file so I can kill the process with WMI or something like that? I would like to do this without logging people out.
If it is impossible then is there a way, from python, to force logout of all users to to get a system where my script can keep working without waiting for me to show up and kill process/log out users?
Any suggestions at all are greatly welcome. I am not an experienced windows programmer.
So it looks like sysinternals provides the solution.
handle.exe does the job.
Do a system call (subprocess module in python for example) and search the output for the directory you are trying to delete. Get the pid and then kill the process with another system call to psKill.exe.
I have not written the code yet but I went through the procedure on the command like and it works. Should be trivial to script this with any programming language doing system calls to the psTools.
Related
This question already has an answer here:
Execute commands in background
(1 answer)
Closed 3 years ago.
I'm connected to a Raspberry Pi running Raspbian lite, using PuTTY with SSH. To execute a Python script, I'm navigating to the script's directory and using python3 scriptname.py, (this script is always running, unless being modified) after doing this, all I can see is the script's console log, and I don't think I'm able to do anything else with the Pi, unless if I stop the scripts execution.
Is there anyway that I can send that process to the background, and continue to use the interface to do other things
I appreciate the different solutions. I don't know why it didn't occur to me that opening a new terminal session would truly act as a new session, I would've guessed it would essentially clone the output, but thanks Mark. Thanks coderasha, I might use Linux Screen sometime. Also, thanks Dinko, I wasn't aware I could append executions using &!
This question already has answers here:
How do you run a python script from within notepad++? [duplicate]
(6 answers)
Closed 7 years ago.
I'm having trouble executing my programs in Notepad++. I'm currently operating on Windows 7.
When attempting to run the program in the the interpreter, I have to jump through numerous hoops to actually get my program to execute. In Notepad++, I'm unable to provide additional text if I'd like to run a sys.argv command, or need to write in a name for a function.
Are there any solutions? Any easier way to run my code?
Run your code from command interpreter.It will look like this
C:\Python27\python.exe programe_name.py
change directory where your program is save before the command.I recommend you to download IDLE editor.It is easy way to learn python.
What I have been doing is this:
Press F5 to run.
Paste this | C:\Python27\python.exe "$(FULL_CURRENT_PATH)"
Replace Python27 with whatever version you use.
Then run.
It should take what you're writing and run it, make sure to select Python as the current language too.
Sorry if this isn't what you're looking for, I'm just starting too!
There are better editors which can run python without a problem. Github's atom can do this easily and is a great free solution and Sublime Text 2 is also both free and paid.
However, if you absolutely have to run on NotePad++, do click Run on the Menu and then Run again. Now, type in the shell command you would like to run (Path-to-python.exe + Path-To-File) and click run
Forgive me if it's a silly question. I'm new to Python and scripting languages. Now I'm using Komodo Edit to code and run Python programs. Each time I run it, I have to wait until the program finished execution to see my "print" results in the middle. I'm wondering if it's possible to see realtime outputs as in a console. Maybe it is caused by some preference in Komodo?
Another question is that I know in the interpreter, when I store some variables it will remember what I stored, like in a Matlab workspace. But in Komodo Edit, every time the program runs from the beginning and store no temporary variables for debugging. For example if I need to read in some large file and do some operations, every time I have to read it in again which takes a lot of time.
Is there a way to achieve instant output or temporary variable storage without typing every line into the interpreter directly, when using other environments like Komodo?
The Python output is realtime.
If your output is not realtime, this is likely an artefact of Komodo Edit. Run your script outside of Komodo.
And Python, as any programming language, starts from scratch when you start it. How would it otherwise work?
If you want a interpreter-like situation you can use import pdb;pdb.set_trace() in your script. That will give you an interpreter prompt for debugging.
I'm making a drawing program with python and pygame. I am trying to incorporate a script-fu thing in which the program opens a python live interpreter upon startup and allows the user to execute commands in the interpreter alongside the graphical interface.
My current strategy is to run the main loop inside its own thread, then have the application opened using a bash script that does 'python -i main.py'
Is this a safe/effective/ideal way of doing this? How can I use locks to ensure that commands coming in from the interpreter are executed between main loop iterations?
This is my first time using threads, so please explain to me like I am 7.
Thanks :)
The interpreter won't cooperate with locks you set (since it doesn't know about them). Thus, you cannot guarantee when the code entered by the user will execute.
Consider using the code module to build your own interactive console (it's really easy!). Then you can do the locking every time you go to execute user input.
Why are you using a third party live interpreter? Do you realize that pygame comes with one built in? The documentation is here. This will eliminate all of your problems quite easily.
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.