A
A
Alexey Denisov2020-08-05 20:21:32
Python
Alexey Denisov, 2020-08-05 20:21:32

How to write an if condition?

Tell me, you need to create directories, how to make a condition so that if returns false if the directory exists, in order to immediately call mkdir (), without else ?

An instruction like != Doesn't work for me, I don't know how to apply it, or is there another way?
To create directories, if one already exists, then display a message and work further (add Files to this directory)

if os.path.exists(path):
   print ('Каталог существует')
else:
   os.mkdir(path)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sswwssww, 2020-08-05
@Denisov80

if not os.path.exists(path):
    os.mkdir(path)

M
Morgenstern, 2020-08-05
@Groiznyi_Project

import os
name = input("Folder name: ")
try:
....os.mkdir(name)
except OSError:
........print("Folder named "+name+" already exists")
Instead of . space
After the Exception processes the request and the folder already exists, it will write that such a folder with Name () has already been created and the folder will not be overwritten !!!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question