0

I wish to create a directory with relative path.

Here is my code:

 if os.path.isdir("Imagettes") is False : os.mkdir("Imagettes") os.makedirs("Imagettes/%Y-%m-%d_%H:%M:%S") 

However it will return an error:

 File "D:\Anaconda3\envs\prunes\lib\os.py", line 223, in makedirs mkdir(name, mode) OSError: [WinError 123] The file, directory, or volume name syntax is incorrect: 'Imagettes/%Y-%m-%d_%H:%M:%S' 

The directory "Imagette" will be correctly created however the date directory I want to create inside won't. I don't get what's wrong. Can somebody help me find out?

4
  • 1
    Are you intending to format the actual date into the name? Also note if you use makedirs you don't need to explicitly create Imagettes/. Commented May 24, 2022 at 14:48
  • If the inner directory is %Y-%m-%d_%H:%M:%S, maybe it's because you wrote '/' and not '\' (assuming you are using windows) Commented May 24, 2022 at 14:50
  • Thank you for your answer. I tried using \ instead of / but it didn't change a thing, the same error occurred. I am indeed working on Windows. Commented May 24, 2022 at 15:15
  • Colons in path names will cause extremely confusing problems on Windows: stackoverflow.com/a/25896961/1084416 Commented May 24, 2022 at 15:21

2 Answers 2

1

A filename cannot contain any of the following characters: \ / : * ? " < > |. So you should replace : symbol, for example:

import os from datetime import datetime s = datetime.now().strftime("%Y-%m-%d_%H.%M.%S") os.makedirs(f"Imagettes/{s}",exist_ok=True) 
Sign up to request clarification or add additional context in comments.

1 Comment

Technically a filename can contain a colon on windows but it creates a metadata filestream: stackoverflow.com/a/25896961/1084416
0

I found a way to have it work. I modified my code as follows :

if os.path.isdir("Imagettes") is False : os.mkdir("Imagettes") today = datetime.now() os.mkdir("Imagettes/"+ today.strftime("%Y%m%d%H%M%S")) 

Hope it can be of use to somebody.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.