Open() in Python Does Not Create a File If It Doesn't Exist

open() in Python does not create a file if it doesn't exist

You should use open with the w+ mode:

file = open('myfile.dat', 'w+')

Python open(file, w+) not creating a nonexistent file

You comments on initial post clarify what you need to happen.

Below assumes that

  • The directory Users/root1/ exists
  • You are trying to create a new sub directory + file inside it
import os

# Wrap this in a loop if you need
new_dir = 'test' # The variable directory name
new_path = 'Users/root1/' + new_dir + "/"
os.makedirs(os.path.dirname(new_path), exist_ok=True) # Create new dir 'Users/root1/${new_dir}/

with open(new_path + "test.txt", "w+") as f:
# Create new file in afore created directory
f.write("test")

This creates a new directory based on a variable new_dir and creates the file test.txt in it.

In Python, how to open a file for writing, but do not create it if the file does not exist?

There's two ways to do this.
Either

from os.path import exists
if exists(my_file_path):
my_file = open(my_file_path, 'w+')

if you need to trigger things based on the file existing, that's the best way.

Otherwise, just do open(file_path, 'r+')

Python doesn't create file if not existing

Use a double backslash:

f=open('c:\\Lets_Create_Malware\\output.txt', 'w+')

From the docs:

The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.

How to open a file for reading and writing, create it if doesn't exist yet, and if it does, then without truncating it?

With a little low-level helper:

import os

def open_create(name, flags):
return os.open(name, flags | os.O_CREAT)

with open("./testfile", 'r+', opener=open_create) as f:
... read/write ...

Writing to a new file if it doesn't exist, and appending to a file if it does

It's not clear to me exactly where the high-score that you're interested in is stored, but the code below should be what you need to check if the file exists and append to it if desired. I prefer this method to the "try/except".

import os
player = 'bob'

filename = player+'.txt'

if os.path.exists(filename):
append_write = 'a' # append if already exists
else:
append_write = 'w' # make a new file if not

highscore = open(filename,append_write)
highscore.write("Username: " + player + '\n')
highscore.close()

Getting a File doesn't exist error when trying to create a file

Since today is a string containing / characters, day = open(today+".txt", "x") will read this string as a path to create the file in.
Your today variable contains 6/11/2021, so it will look for the directory structure 6/11 and in that folder it will try to create the file 2021.txt. Since he can't find those directories, the instruction will fail. You can try to format your file name 6-11-2021.txt. If you do want to use the directories, you can parse today and create directories using os.mkdir



Related Topics



Leave a reply



Submit