I have encountered this problem before. My problem was that I had to write to a file and then use that name as an argument in the command.
The reason this works on Linux is because @PM 2Ring says in the comments, Linux allows multiple processes to write to the same file, but Windows does not.
There are two approaches to solving this problem.
One of them is to create a temporary directory and create a file in this directory.
# Python 2 and 3 import os import tempfile temp_dir = tempfile.mkdtemp() try: temp_file = os.path.join(temp_dir, 'file.txt') with open(temp_file, 'w') as f: pass
# Python 3 only import tempfile with tempfile.TemporaryDirectory() as temp_dir: temp_file = os.path.join(temp_dir, 'file.txt') with open(temp_file, 'w') as f: pass
Another approach is to create a temporary file with delete=False so that when you close it, it will not be deleted, and then manually deleted it later.
# Python 2 and 3 import os import tempfile fp = tempfile.NamedTemporaryFile(suffix='.txt', delete=False) try: fp.close() do_stuff(fp.name) finally: os.remove(fp.name)
Here is a small context manager that can create files:
import os import tempfile _text_type = type(u'') class ClosedTemporaryFile(object): __slots__ = ('name',) def __init__(self, data=b'', suffix='', prefix='tmp', dir=None): fp = tempfile.mkstemp(suffix, prefix, dir, isinstance(data, _text_type)) self.name = fp.name if data: try: fp.write(data) except: fp.close() self.delete() raise fp.close() def exists(self): return os.path.isfile(self.name) def delete(self): try: os.remove(self.name) except OSError: pass def open(self, *args, **kwargs): return open(self.name, *args, **kwargs) def __enter__(self): return self.name def __exit__(self, exc_type, exc_val, exc_tb): self.delete() def __del__(self): self.delete()
Using:
with ClosedTemporaryFile(suffix='.txt') as temp_file: do_stuff(temp_file)