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


Messages In This Thread
Error on open of file created with tempfile.TemporaryDirectory() - by Brian177 - Apr-04-2021, 02:49 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Open file, and insert space in string? Winfried 7 622 May-28-2025, 07:56 AM
Last Post: Winfried
  Trying to open depracated joblib file mckennamason 0 942 Sep-19-2024, 03:30 PM
Last Post: mckennamason
  File is not being created with 'w' argument CAD79 3 1,741 Mar-14-2024, 12:05 PM
Last Post: snippsat
  Open/save file on Android frohr 0 1,269 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 10,424 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 2,168 Aug-14-2023, 05:04 PM
Last Post: snippsat
  Coding error. Can't open directory EddieG 6 2,631 Jul-13-2023, 06:47 PM
Last Post: deanhystad
  I cannot able open a file in python ? ted 5 12,396 Feb-11-2023, 02:38 AM
Last Post: ted
  testing an open file Skaperen 7 2,971 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 2,197 May-06-2022, 01:44 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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