I'm having to use a script temporarily in windows, rather than my usual Ubuntu. I have found that the script fails to run due to calling "tempfile" not working in windows. I've attached the snippet below. where I get the error
File "phys_features_windows.py", line 70, in extract_LCR with open(tmp_LCR.name, 'w') as f_LCR: PermissionError: [Errno 13] Permission denied: 'C:\Users\Henry\AppData\Local\Temp\tmpz6wx32mx'
I've also tried the tweak suggested of calling "delete=False" but this just changed the error to
FileNotFoundError: [WinError 2] The system cannot find the file specified
in the "out" line
I assume that the file creation works differently in windows, or the way the file is called does, and so the script can't find the temporary file - but unfortunately I can't find the solution after further testing and reading. Any help would be greatly appreciated!
Many thanks
original code:
def extract_LCR(seq):
tmp_LCR = tempfile.NamedTemporaryFile(delete=False, delete_on_close=False) #flag for windows
with open(tmp_LCR.name, 'w') as f_LCR:
f_LCR.write('>1\n' + str(seq))
tmp_LCR.seek(0)
out = subprocess.Popen(['segmasker', '-in', str(tmp_LCR.name)],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
with line 2 changed to
tmp_LCR = tempfile.NamedTemporaryFile(delete=False, delete_on_close=False) #flag for windows
producing the second error.
I am happy to provide more of the code if needed but the script is long, and I'd prefer not to attach the whole thing!
Many thanks!
Windows won't let you open a file for writing that is already open.
NamedTemporaryFilegives you a writable file object. Use it in awithclause and close the file before making the subprocess call to make sure the file has been flushed and is usable by the called program. This takes you back to the defaultdelete=Trueso that your temporary file is cleaned up when thewithterminates.