I've run into this problem before. My problem was that I had to write to a file and then use that file's name as an argument in a command.
The reason this works in Linux is that, as @PM 2Ring said in the comments, Linux allows multiple processes to write to the same file, but Windows does not.
There are two approaches to tackle this.
One is to create a temporary directory and create a file in that 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 # Create the file, or optionally write to it.
try:
do_stuff(temp_file) # In this case, open the file in an editor.
finally:
os.remove(file_name)
finally:
os.rmdir(temp_dir)
# 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 # Create the file, or optionally write to it.
do_stuff(temp_file)
# with tempfile.TemporaryDirectory(): automatically deletes temp_file
Another approach is to create the temporary file with delete=False
so that when you close it, it isn't deleted, and then delete it manually 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 little context manager that can make 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()
Usage:
with ClosedTemporaryFile(suffix='.txt') as temp_file:
do_stuff(temp_file)