Read from "locked" file in windows using python - python

I have written a python script to process the output file of another program and run various stats on it. Right now, when I attempt to access that file from my python script:
with open('C:\\my_file_path', 'rb') as outfile:
print(outfile)
I receive an error message:
PermissionError: [Errno 13] Permission denied: 'C:\my_file_path'
When using other programs (specifically HxD, the hex editor), Windows gives a more verbose error popup stating:
The process cannot access the file because it is being used by
another process.
Running the program as an Administrator, or with sudo from within WSL Ubuntu does not make any difference.
Is there any way to read the data which is being written to this file despite these locking conditions? I cannot mess with the first program as it's a low-level device driver for which I do not have source code. It essentially records data from a hardware sensor and writes it to a file for several hours so being able to concurrently parse that file in python (rather than waiting until the hours-long recording is over) would be much better.

Related

Python script cannot find file only sometimes

I am running a Python script multiple times and the only thing that changes each time is the name of my run.
Approximately 50% of the time, the runs fail and I get the error output:
IOError: Cannot find file filename.txt
But the file is certainly in that location when I check. And the same file is also used by all the other runs that succeed.
So I'm just trying to understand what if there is anything that could be producing this error only sometimes.
I don't know if it's relevant but I am submitting the jobs via Slurm on a remote machine.

Cannot get Perforce Triggers to work properly

Been trying to get this working all day, and i just can't figure out why its not working.
Trying to implement a simple trigger to run when the user submitted a file.
example in the .tmp file:
hello_trigger change-submit //testDepot/... "python F:/triggers/hello_trigger.py"
when i try to submit a file i get this:
Submit validation failed -- fix problems then use 'p4 submit -c 10199'.
'hello_trigger' validation failed: python: can't open file 'F:/triggers/hello_trigger.py': [Errno 2] No such file or directory
File exists and can be read, so its not a python issue.. same error with a .txt or .bat file.
From what i can gather the problem seems to be coming from the depot line in the trigger.
//testDepot/... fails
//depot/... doesnt fail, but the script is never fired off.
Any suggestions are greatly appreciated.
also testDepot is a stream not sure if that matters.
python: can't open file 'F:/triggers/hello_trigger.py': [Errno 2] No such file or directory
seems pretty clear that the file doesn't exist, at least from the point of view of this trigger command. Some things to double check:
This is running on the server machine, i.e. the place where the p4d service is running. If you have the script on your client machine the Python executable on the server isn't going to be able to find it!
Similarly, this is being run by whatever user is running p4d (on Windows this is usually the SYSTEM user, which may have limited permissions). Does that user have permission to read this path?
Could it be that your version of Python on Windows doesn't know how to handle paths with Unix-style forward slashes? (Many tools will normalize these for you but you shouldn't depend on it!) Try using a valid Windows path, i.e. F:\triggers\hello_trigger.py.

How do I close an existing file that I created with my Python script, every time I run said Python script?

In my script, I had created a CSV file using Pandas and had it automatically opened using os.startfile(). I'm wondering what I need to do to my script to allow this CSV file to close, and then reopen, with the updated information so that I don't have to manually go into Excel and close the file every time I rerun the script.
Every time I run my script, if my .csv file is open, I get the following error message:
PermissionError: [Errno 13] Permission denied: "filename.csv".
I DON'T get this error message when the file is closed.

Windows permission error to delete joblibmem mapping folder in python

I am trying to run a python code that performs XGboosting and I wanted it to run parallely to take less time in building a model. But I am facing this issue while running a code.
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\<<username>>\\AppData\\Local\\Temp\\joblib_memmapping_folder_85680_9566857635\\85680-1746225537432-968de5958f0642829c37f0f09f0e8b00.pkl'
I have even tried running the anaconda prompt as an administrator. But it is of no use. As a workaround I have also tried what is suggested in https://github.com/joblib/joblib/issues/806 but even then I am facing the same issue.
Could you please advice?

File sharing problem with Python inside Excel

I have python 2.6 script that creates a bunch of csv files on windows.
This script can be run stand alone or inside Excel via VBA shell command.
There are no problems when runs as stand alone, followed by VBA script.
When I run script inside Excel with a shell call. I have file sharing problems.
The Script creates runs, close files
fw = open(fn, "wb")
fw.write(....)
fw.close()
at the end of script I have:
os._exit(1)
Then Excel VBA does its stuff with the files. This gives error messages.
The error msg:
"FILE Now Available"
....is now avaiable for editing.
Choose read-write to open it for editing.
The script is multitheaded....
You may want to create a delay after the shell call (doEvents or sleep(100) might work) to allow the OS to properly close the file and remove all pointers to the file.

Categories

Resources