Close an excel file using python - 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

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

How to close the display of the file

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.

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

Process Accessability

I am trying to write some information into the file, which is located in
ftp, and this file can be updated by number of people, but if at all i
download a file from the ftp to my local machine, update it and then
upload it back to ftp, and at the same time if some one else downloads
the same file for the modification, then the data will be overwritten.
so is there a way in Python script where i can lock the file, so that
no one updates it until i release the lock
Look at fuser
It is unclear if you need to know for debugging purposes or to support a feature in the code but this is the method call to get the pid of the current executing process.
import os
print "Process id:",os.getpid()
This will simply dump the id to output and you can put other information of interest there so you can follow which is what.
Is this what you are looking for? If not, please clarify and I'll try again.

Categories