Trying to determine if a certain excel file is already open. I have a script that opens up a template excel file and writes data to the file and then saves it as a specific formatted name. Now if the person runs the script again and forgets to close out of the excel file I get errors that stop the program saying cant save the file as it is already open. Is there a way to check if, not only a program is open (excel) but a specific file? That way I can prompt the user to either close the file or save it as another filename.
If the processing time of the input is really small you do not need to detect this before processing the file. You can easily catch the error that you describe with "I get errors that stop the program saying cant save the file as it is already open" and provide a meaningful error message to the user.
Related
I have a batch file scheduled to run a python script every morning. The script is supposed to append to a .csv file. However, some days no data is appended to the csv file which I believe is due to an error during execution of the script. Is there anything I can add to my batch file to save any error messages that cause the script to stop execution to a .txt file? Or is there a better way to do this within the python script instead? Right now in my batch file I just have
python "C:\filepath\pythonscript.py"
Append redirection to command line, as displayed bellow:
python "C:\filepath\pythonscript.py" 2>>log.txt
2>>log.txt statement redirect STDERR (errors printed by python interpreter) to a log file in APPEND mode (single > will switch to OVERWRITE mode). If you want to capture STDOUT too, change added apendix to >>log.txt 2>&1
I got a program that using 2 different language, MQL5 and Python. As a bridge between 2 script, I'm using 2 text file. MQL5 will write a file. Python will standby and periodically check if said file exist. If it indeed exist, Python will read the file then write another file, before deleting file that have been read. MQL5 after writing the file will go to standby and periodically check if Python have produced a reply. If reply filename exist, it will read said file.
Unfortunately, MQL5 keep trying to read the reply file while it being written by Python. It cause MQL5 to throw error and if I force it to read it, it will read blank file. Is there anyway to avoid this? Is there anyway to detect if a file have finish being modify by another program in MQL5?
Below is the code I used to try to handle this problem to no avail.
while(!FileIsExist("output.txt"))
{
}
if(FileIsExist("output.txt"))
{
ResetLastError();
do
{
int file_handle=FileOpen("output.txt", FILE_READ|FILE_SHARE_READ|FILE_TXT);
}
while(file_handle==5004);
}
two parts:
Using python, I want to write everything from stdout to a log file. I found a good solution for this part, but,
I'm also looking for a way to delete the file when it gets too big and start a new file. Usually the info going to the display is not useful so I don't care about it, but when an error occurs, I'd like to have captured it to a file to see what lead up to it.
I could write a function that checks the file size and starts a new file when the current file reaches a particular size, but I'm wondering if something exists to do this.
I would like my test program to run for weeks, constantly outputting to the display and a text file, but creating a new text file when the current one gets too big, so basically a circular buffer that wraps around.
I'm trying to create a script (Script #1) that writes a file that can only be read once globally, and then another script (Script #2) that reads this file only if it was never read before in the world.
Example Situation:
I create a CSV file with my Script #1 and email this CSV file to 10 people, who are on different computers.
All 10 try to run this file with my Script #2:
Expected behaviour:
The first person in the world to run Script #2 with this file gets a message saying they are the first person to read this file and can actually see the content.
2nd -10th person that try to read the file get a message saying someone has already read it before and can't access the file.
How can I accomplish this?
This is not something very serious, so I'm not really worried about security of the process, but want it to work.
Not Giving the whole process because it will be a large explanation but you can do the below mention points
Encrypt the file first.
Second the script which you want to read the content of your file must be linked with a server application from which it will receive decryption key and send data to server about whether the file is read before or not. It's like if the file readed earlier then the server don't send the decryption key.
Then after receiving the key the script should decrypt the file and read it.
Hi I am checking to see if a excel file is modified, and if it is basically save it as something else and open it. So it works the first time around, but on the second time I modify the file, I am getting error: The system cannot find the file specified: 'C:\example.xlsx'
Sometimes it would also throw: Permission denied: 'C:\Todolist2.xlsx'
Please help. Newbie here. Thank You
import time, os.path, os, openpyxl
from openpyxl import Workbook
currentFD = os.stat("C:\\example.xlsx")
while True:
modDate = os.stat("C:\\example.xlsx")
if (modDate > currentFD):
print('yes it does')
wb=openpyxl.load_workbook("C:\\example.xlsx")
wb.save("C:\\Todolist2.xlsx")
os.startfile("C:\\Todolist2.xlsx")
currentFD = modDate
You seem to have two different problems here but they may be related.
Since you gave a traceback for the Permission Denied error on C:\Todolist2.xlsx, let's look at that one.
On Windows, many programs, when they open a file, put a lock on it. This is especially true for "applications" programs, like Excel and Notepad. If one program has a file locked, any another program that tries to open that file to overwrite it will get a permissions error.
And that's exactly what you're seeing: The first time through, your code overwrites Todolist2.xslx, then uses startfile to tell Excel (or some application that's registered for Excel files) to open it, which works. Then it tries to overwrite the same file, which Excel presumably still has locked and open, which fails.
Depending on what you're actually trying to do here, there are a few possible workarounds:
Copy Todolist2.xlsx to a temporary file, then start that temporary.
Create new files Todolist2.xlsx, Todolist2-1.xlsx, etc., and keep opening them.
Use either COM automation or a GUI scripting framework like autogui to make Excel open a copy of the file rather than opening the file itself.
Use either of the above to make Excel close the file before overwriting the file and launching it.
Launch a new Excel instance to open the file using subprocess.Popen, so you can kill it and launch a new one.
Rewrite your whole code to build the spreadsheet using Excel COM automation, rather than by building a file to pass to it.