How to close the display of the file - python

I'm using os.startfile(filename) to open (display) a file. I'm unable to find any command to close the display of this file.
Please, let me know, if there's any corresponding command or function for this.

According to the docs, there is no way -- https://docs.python.org/2.7/library/os.html#os.startfile
startfile() returns as soon as the associated application is launched.
There is no option to wait for the application to close, and no way to
retrieve the application’s exit status.
You will need to revert to pywin.

Related

Closing mp4 video in python [duplicate]

I've read that there is a way to open a file in python with..
os.startfile('file.exe')
is there a way to close the same file when open?
Thank you in advance!
From the os.startfile() doc:
startfile() returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application’s exit status.
So, basically, no, there isn't a way to close a file opened with startfile.
It isn't clear from the question is whether you want to launch a file, or to open it (for reading/writing).
If you want to launch a process, subprocess is a better candidate for running other processes and controlling them through a subshell (including killing them.)
If you want to open a file for read/write, then open() would be a good choice to start with.
As the Python Wiki says this function does the following:
Start a file with its associated application.
So the best idea would be to use os.kill to kill the application in which it is opened. The problem lies in identyfying what application is associated with the file of following extension and finding pid of exact instance which opened that file.
You have used .exe file in example which is executable file extension, so you probably misunderstood what this function does. What are you trying to accomplish? Are you sure it is the correct way of doing it?
If you really want to launch executable file, you should probably use os.system(). If you want to create a new file, write something in it and close it, look for python file operations, here are good examples: http://www.tutorialspoint.com/python/python_files_io.htm

Close an excel file using python

I want to close an excel file after opening it. I searched this site but I cannot get a solution. Can anyone help?
import os
#TODO: close the file 'myexcel.xlsx' before writing
#writing data to 'myexcel.xlsx' here
a = os.startfile('myexcel.xlsx')#open the file for reading
Thank you very much.
This isn't possible using startfile(). According to the os.startfile documentation:
startfile() returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application’s exit status.
You might consider using the subprocess module. There are a load of options depending on what exactly you're trying to do at this question: How to terminate a python subprocess launched with shell=True

Using the Output of Sysinternals Process Monitor in another programm/script in real time

I'm working on a script that should check on certain system events (like opening of a file, or changing of a registry key) and start further actions depending on that. But I haven't found a clean way to get the information into my script.
I'm looking for a way to get the output of Sysinternals Process Monitor into another program. This should happen without user interaction in close to real time; so saving into a CSV/XML and than using this doesn't work.
I've checked on using the backing file, but this is in the Process Monitor PML format, which i haven't found to be documented anywhere.
Does anybody know a way how I can get the output of Process Monitor into my script?
Or an other (not too messy) way to get a real time list of opened files, registry keys etc into a python program?
Thanks!
If you want to parse stdout or a file, and your ok with a 32 bit only solution, try Dr Strace or ntstrace.
YOu could also look into ospy or another ProcMon alternative. ospy is open source, so at the very least you could look at the source code for capturing events.
Here is a list of alternates to ProcMon.

Closing a file python

I've read that there is a way to open a file in python with..
os.startfile('file.exe')
is there a way to close the same file when open?
Thank you in advance!
From the os.startfile() doc:
startfile() returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application’s exit status.
So, basically, no, there isn't a way to close a file opened with startfile.
It isn't clear from the question is whether you want to launch a file, or to open it (for reading/writing).
If you want to launch a process, subprocess is a better candidate for running other processes and controlling them through a subshell (including killing them.)
If you want to open a file for read/write, then open() would be a good choice to start with.
As the Python Wiki says this function does the following:
Start a file with its associated application.
So the best idea would be to use os.kill to kill the application in which it is opened. The problem lies in identyfying what application is associated with the file of following extension and finding pid of exact instance which opened that file.
You have used .exe file in example which is executable file extension, so you probably misunderstood what this function does. What are you trying to accomplish? Are you sure it is the correct way of doing it?
If you really want to launch executable file, you should probably use os.system(). If you want to create a new file, write something in it and close it, look for python file operations, here are good examples: http://www.tutorialspoint.com/python/python_files_io.htm

pexpect output in different window

Now I am working in a project where the testscript has to connect many (3-10) remote computers (SSH and do some stuff).
I started to use the pexpect and it is simple as a button. It works fine.
I want to see the communication during test. I know it is possible to redirect the log to the screen. But in this case the logs (from different computer) are mixed.
What I would like is to open new terminal window (or consol or whatever) for every new spawn object. In this case I could see all communication in different windows. Additionally I would like to keep the possibility of spawn.interact() in every window.
I feel that it is possible somehow but I don't know how. I think some file pointer (or pipe) should pass to the new window somehow(?)
(SecureCRT knows sometihng like this, it has tabbed consol windows and can access them separately, but it is a commercial product)
Or let me make the problem more simple.
If I do this, I can open a new shell in a new window:
p=Popen(["cygstart", "bash"])
How can I read and write into this shell from my script (parent) to see it in this new window?
I would really appreciate it, if one of you could point me in the right direction.
It is enough if you tell me what to read or find for (on Google) because I did not find anybody such kind of problem.
The environment is cygwin.
Thanks in advance
br:drv
Have you tried using the logfile parameter?
child = pexpect.spawn('some_command')
mylog = open('/tmp/mylog','w')
child.logfile = mylog
This will automatically log all communication to the file, including commands you enter after calling spawn.interact()
More info available on the website: http://pexpect.sourceforge.net/pexpect.html
Search for 'logfile' to find the relevant documentation.

Categories