Python Forum
Error on open of file created with tempfile.TemporaryDirectory()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error on open of file created with tempfile.TemporaryDirectory()
#1
The below code gives an error on the line: tfp = open(path, "w+t")
Anyone know why?

Error:
Exception has occurred: NotADirectoryError [WinError 267] The directory name is invalid: 'C:\\Users\\gener\\AppData\\Local\\Temp\\tmp99c64lys\\tempfile.txt' File "C:\Users\gener\OneDrive\My Documents\Python Programming\run.py", line 10, in <module> print(tfp.read())
import os
import tempfile

# create a temporary directory using the TemporaryDirectory class
with tempfile.TemporaryDirectory() as tdp:
    path = os.path.join(tdp, "tempfile.txt")
    tfp = open(path, "w+t")
    tfp.write("This is a temp file in temp dir")
    tfp.seek(0)
    print(tfp.read())
Reply
#2
The exception traceback does not seem to agree with your claim that the code fails on the line tfp = open(...). Can you post the exact code and the full traceback?
Reply
#3
You've set up a context for the temp directory, but not for the tfp file. So when the end of the code is reached, the tfp file isn't immediately closed.

On linux, an open file doesn't matter, you can still delete the file and the directory. But on Windows, the removal will fail.

Either explictly close() the tfp file, or open it in a with context so that it is closed before the end of the TemporaryDirectory context.
Gribouillis likes this post
Reply
#4
In other words, there is not a problem with line 10 other than it being the last line of the context created by this:
with tempfile.TemporaryDirectory() as tdp:
When your program finishes line 10 it exits the context defined for tdp and calls the exit function. The exit function deletes the directory, unsuccessfully.

Why aren't you using TemporaryFile() instead of TemporaryDirectory()?
Reply
#5
Thank you all! Adding the tfp.close() resolved it as I am on Windows 10.

The code is from a video Python course I am taking. The code works for the instructor in the video, and he is on Linux, or a Linux like platform. The comment about it not mattering on Linux explains it.

In the same lesson he had demonstrated tempfile.TemporaryFile just before a demo of tempfile.TemporaryDirectory.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File is not being created with 'w' argument CAD79 3 318 Mar-14-2024, 12:05 PM
Last Post: snippsat
  Open/save file on Android frohr 0 277 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 938 Dec-14-2023, 08:03 AM
Last Post: shanoger
  How can i combine these two functions so i only open the file once? cubangt 4 804 Aug-14-2023, 05:04 PM
Last Post: snippsat
  Coding error. Can't open directory EddieG 6 1,062 Jul-13-2023, 06:47 PM
Last Post: deanhystad
  I cannot able open a file in python ? ted 5 3,043 Feb-11-2023, 02:38 AM
Last Post: ted
  testing an open file Skaperen 7 1,300 Dec-20-2022, 02:19 AM
Last Post: Skaperen
  I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str decoded 1 1,360 May-06-2022, 01:44 PM
Last Post: Larz60+
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 5,876 Jan-07-2022, 04:55 PM
Last Post: bowlofred
  Error when using Watchdog and Qt to keep open a Queue before and after sending first pyhill00 0 1,546 Oct-28-2021, 09:10 AM
Last Post: pyhill00

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020