I want to use tempfile
module, to generate files and keep them in /tmp/myfolder/
and keep them there until a certain time (maybe in days). I learned that tempfile
removes file as soon as f.close()
is run. I just don't want it to auto-remove. Will remove later. Is this possible with tempfile
? or any other better approach?
Asked
Active
Viewed 3,228 times
4
-
You are, of course, free to make a folder in `/tmp` and write/maintain your own files in there. `tempfile` does not sound suitable for your use-case anyway. Also, I wrote a module [seqfile](https://pypi.python.org/pypi/seqfile), which allows me to write to files on disk without worrying about overwriting files being written by other threads/processes. – musically_ut May 01 '16 at 15:16
-
Maybe what you want is [`tempfile.NamedTemporaryFile`](https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile) – styvane May 01 '16 at 15:18
-
1If you want to retain files then it is not a good idea to store them in `/tmp`. – cdarke May 01 '16 at 15:19
-
Do you want your code to also auto-delete them after
in days? (Alternatively you could use Unix `cron`, or an inverted `find -newer ... | xargs` or something) – smci Jul 20 '17 at 08:56
2 Answers
4
You can use this:
import tempfile
with tempfile.NamedTemporaryFile(delete=False, dir='/tmp/myfolder') as outfile:
# ...

John Zwinck
- 239,568
- 38
- 324
- 436
-
1
-
@smci: Please don't speculate about what OP might have wanted a year ago. If you have related questions, feel free to post them separately. – John Zwinck Jul 20 '17 at 09:33
-
7not speculation. Read what the OP wrote **"keep them there until a certain time (maybe in days).... I just don't want it to auto-remove [at file close]. Will remove later"**. As such, your answer doesn't fully address their requirements. – smci Jul 21 '17 at 07:48
-
2JohnZwinck your answer does not fully address the OP's question. It's utterly irrelevant whether it was asked 1 hour ago or 1 year ago. Also the fact that the OP did not return to SO since 5/2016 to reply and point out that it didn't address it is not relevant. Auto-deleting tempfiles after a time threshold in Python is interesting and useful and I was genuinely looking forward to seeing a solution. Who that "would be helping" would be all Python users with a similar issue. There's absolutely no need to talk down to me. – smci Jul 21 '17 at 13:11
-
I think you should post your own question (you can even answer it as well). You could also add an answer to this question if you feel it would better address the original question. – John Zwinck Jul 22 '17 at 01:40