1

I would like to create a temporary file with an specific name (and if its possible with an specific extension).

Example:

-mytempfile.txt
-mytempfile2.xml

I've been reading about tempfile library, but as far as I know I can only set the following parameters

(mode='w+b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None)
6
  • 1
    If you do fopen on a file which doesnt exist, it will create it. You can delete it at the end of your program Commented Aug 9, 2018 at 21:09
  • What have you tried ? Commented Aug 9, 2018 at 21:10
  • @RemyJ OP showed effort by listing tempfile package and concerning about lost parameter. Commented Aug 9, 2018 at 21:12
  • 1
    Check NamedTemporaryFile. It doesn't provide name specification, but you can use generated name to open the file a second time. Commented Aug 9, 2018 at 21:12
  • @vishes_shell OK, it's just that I was expecting a some code. Commented Aug 9, 2018 at 21:14

1 Answer 1

8

The most secure method to do what you are asking for is, as Dan points out there is no need to specify any name to the file, I am only using suffix and prefix as OP asked for it in the question.

import os
import tempfile as tfile
fd, path = tfile.mkstemp(suffix=".txt",prefix="abc") #can use anything 
try:
    with os.fdopen(fd, 'w') as tmpo:
        # do stuff with temp file
        tmpo.write('something here')
finally:
    os.remove(path)

to understand more about the security aspects attached to this you can refer to this link

well if you can't use os and are required to perform these actions then consider using the following code.

import tempfile as tfile
temp_file=tfile.NamedTemporaryFile(mode="w",suffix=".xml",prefix="myname")
a=temp_file.name

temp_file.write("12")
temp_file.close()

a will give you the complete path to the file eg as:

'/tmp/mynamesomething.xml'

in case you don't want to delete the file in the end then use:

temp_file=tfile.NamedTemporaryFile(delete=False) #along with other parameters of course.
Sign up to request clarification or add additional context in comments.

5 Comments

Note that you don't have to care about the name of the file. In fact, it's critical that you not specify a full temporary file name to make sure the name is and always will be unique. Using tempfile is a strong suggestion, because it automatically guarantees tempfile uniqueness, and it also automatically cleans up after itself.
@Dan Farrell thanks for the suggestion, made edits to include this important point.
Thanks@Inder I'll take a look at the link you provided me. Also if you guys feel like getting the whole picture. I just explain why I want to create temporary files with specific names, on a comment to the original post.
@jajdp updated my answer kindly check if it satisfy your requirements
Thanks for your updated answer, actually I'm currently finding new ways to achieve what I want. Your code is helping me understand this better. I'm giving you a thumb up for the moment. This might take a few days... I'll update this post when I have a solid answer and understanding of what would be the best way to do this. Thanks~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.