Python Forum

Full Version: OSError: [Errno 26] Text file busy: '/var/tmp/tmp5qbeydbp
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey,

Im using python3 and i have this code :
                import tempfile
                temp_file = tempfile.mkstemp(dir="/var/tmp")
                with open (temp_file[1], "w+b") as tf:
                    tf.write(result.content)
                    tf.close()
                os.chmod(temp_file[1], 0o755)

                if 'args' in command[cmd_type]:
                    args = [temp_file[1]] +  command[cmd_type]['args']
                else:
                    args = [temp_file[1]]
        result = subprocess.run(
            args,
            stdout=subprocess.PIPE,
            universal_newlines=True,
        ).stdout.strip()
Im creating a tempfile that is a binary file and i get this value from code:
/var/tmp/tmp5qbeydbp - that this is the file that has been created and i try to run it
in the last subprocess run but i get this error:

client_1  |   File "/usr/local/lib/python3.7/subprocess.py", line 472, in run
client_1  |     with Popen(*popenargs, **kwargs) as process:
client_1  |   File "/usr/local/lib/python3.7/subprocess.py", line 775, in __init__
client_1  |     restore_signals, start_new_session)
client_1  |   File "/usr/local/lib/python3.7/subprocess.py", line 1522, in _execute_child
client_1  |     raise child_exception_type(errno_num, err_msg, err_filename)
client_1  | OSError: [Errno 26] Text file busy: '/var/tmp/tmp5qbeydbp'
Why is file always busy ? it started when i added the chmod command.
but without it, it doesnt have permission to run.

Thanks.
Hi,
This is indeed weird and I have been thinking it over. I did not try your code but I've read the manual of tempfile.mkstemp(). There I read "... returns a tuple containing an OS-level handle to an open file (as would be returned by os.open())...". This means the "mkstemp()" already opens the file. So it may be spurious that you also open the file (with open (temp_file[1], "w+b") as tf:).
Perhaps you should not open the file but write directly to it (os.write(temp_file[0], result.content)) and then close the file (os.close(temp_file[0])).
Let us know if this works.