How can I create a tmp file in Python?
Asked Answered
D

3

140

I have this function that references the path of a file:

some_obj.file_name(FILE_PATH)

where FILE_PATH is a string of the path of a file, i.e. H:/path/FILE_NAME.ext

I want to create a file FILE_NAME.ext inside my python script with the content of a string:

some_string = 'this is some content'

How to go about this? The Python script will be placed inside a Linux box.

Despumate answered 20/12, 2011 at 14:40 Comment(3)
Open with 'w' and then close it, use os.touch(fname).Latoshalatouche
Why do you need the temporary file then?Ophthalmia
I have a function that calls the FILE_PATH of the object so it needs to be thereDespumate
M
106

There is a tempfile module for python, but a simple file creation also does the trick:

new_file = open("path/to/FILE_NAME.ext", "w")

Now you can write to it using the write method:

new_file.write('this is some content')

With the tempfile module this might look like this:

import tempfile

new_file, filename = tempfile.mkstemp()

print(filename)

os.write(new_file, "this is some content")
os.close(new_file)

With mkstemp you are responsible for deleting the file after you are done with it. With other arguments, you can influence the directory and name of the file.


UPDATE

As rightfully pointed out by Emmet Speer, there are security considerations when using mkstemp, as the client code is responsible for closing/cleaning up the created file. A better way to handle it is the following snippet (as taken from the link):

import os
import tempfile

fd, path = tempfile.mkstemp()
try:
    with os.fdopen(fd, 'w') as tmp:
        # do stuff with temp file
        tmp.write('stuff')
finally:
    os.remove(path)

The os.fdopen wraps the file descriptor in a Python file object, that closes automatically when the with exits. The call to os.remove deletes the file when no longer needed.

Maxwell answered 20/12, 2011 at 14:49 Comment(7)
This should really be updated to provide the secure way of creating tempfiles in python. Here is some examples. security.openstack.org/guidelines/…Harman
Shouldn't you also fd.close() in the finally block? Check the docs for os remove regarding removing files still in use - docs.python.org/3/library/os.html#os.removeAkerboom
@NelsonRodrigues the "with" keyword automatically closes the file aliased after executing the inner codeLexicology
@ganski oh I see, I thought it just closed the tmp object. I tested it and it also closed fd.Akerboom
How do I select the type of file that I am creating?Brent
Also I write to the file but when I try to print(tmp.read()) I get empty field. I changed the 'w' to 'w+'Brent
Add tmp.flush() after tmp.write() before trying to read the temp file.Yolanda
H
257

I think you're looking for a tempfile.NamedTemporaryFile.

import tempfile
with tempfile.NamedTemporaryFile() as tmp:
    print(tmp.name)
    tmp.write(...)

But:

Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).

If that is a concern for you:

import os, tempfile
tmp = tempfile.NamedTemporaryFile(delete=False)
try:
    print(tmp.name)
    tmp.write(...)
finally:
    tmp.close()
    os.unlink(tmp.name)
Hive answered 20/12, 2011 at 14:49 Comment(4)
this is a good answer based on the subject of the question, which google takes note of, rather than SuperString's particular problemMetatherian
and you can pass an optional dir=path arg to NamedTemporaryFile if you want to control which directory it allocates it in.Proterozoic
do you need to do os.unlink(tmp.name)? isn't the point of using tempfile to handle cleanup?Lemmuela
is there any chance that tempfile will create two files with the same name simultaneously? If yes, will the first file get overridden? (consider that I have multiple users which can call the function simultaneously)Roughcast
M
106

There is a tempfile module for python, but a simple file creation also does the trick:

new_file = open("path/to/FILE_NAME.ext", "w")

Now you can write to it using the write method:

new_file.write('this is some content')

With the tempfile module this might look like this:

import tempfile

new_file, filename = tempfile.mkstemp()

print(filename)

os.write(new_file, "this is some content")
os.close(new_file)

With mkstemp you are responsible for deleting the file after you are done with it. With other arguments, you can influence the directory and name of the file.


UPDATE

As rightfully pointed out by Emmet Speer, there are security considerations when using mkstemp, as the client code is responsible for closing/cleaning up the created file. A better way to handle it is the following snippet (as taken from the link):

import os
import tempfile

fd, path = tempfile.mkstemp()
try:
    with os.fdopen(fd, 'w') as tmp:
        # do stuff with temp file
        tmp.write('stuff')
finally:
    os.remove(path)

The os.fdopen wraps the file descriptor in a Python file object, that closes automatically when the with exits. The call to os.remove deletes the file when no longer needed.

Maxwell answered 20/12, 2011 at 14:49 Comment(7)
This should really be updated to provide the secure way of creating tempfiles in python. Here is some examples. security.openstack.org/guidelines/…Harman
Shouldn't you also fd.close() in the finally block? Check the docs for os remove regarding removing files still in use - docs.python.org/3/library/os.html#os.removeAkerboom
@NelsonRodrigues the "with" keyword automatically closes the file aliased after executing the inner codeLexicology
@ganski oh I see, I thought it just closed the tmp object. I tested it and it also closed fd.Akerboom
How do I select the type of file that I am creating?Brent
Also I write to the file but when I try to print(tmp.read()) I get empty field. I changed the 'w' to 'w+'Brent
Add tmp.flush() after tmp.write() before trying to read the temp file.Yolanda
A
-1

Since Python 3.12, there's a proper way to write to a temp file, then read from it (snippet taken from https://docs.python.org/3.12/library/tempfile.html#examples):

with tempfile.NamedTemporaryFile(delete_on_close=False) as fp:
    fp.write(b'Hello world!')
    fp.close()

    # the file is closed, but not removed
    # open the file again by using its name
    with open(fp.name, mode='rb') as f:
        f.read()

# file is now removed since it exits from `with`

If you want to create a text file instead of binary, use tempfile.NamedTemporaryFile(mode='w+t', delete_on_close=False) instead.

Antlion answered 18/2 at 8:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.