Skip to content

Instantly share code, notes, and snippets.

@ByoungInKim
Last active November 21, 2023 03:45
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ByoungInKim/03096da17ff645ae1c4caa2268b2ae9f to your computer and use it in GitHub Desktop.
Save ByoungInKim/03096da17ff645ae1c4caa2268b2ae9f to your computer and use it in GitHub Desktop.
python - create directory if path if it doesn`t exist for file write
import os
directory = '/home/kenny/gist'
if not os.path.exists(directory):
os.makedirs(directory)
@chitown88
Copy link

needs quick fix with the typo *directory in line 2:

directroy = '/home/kenny/gist

@gosuto-inzasheru
Copy link

directory = '/home/kenny/gist'

@ByoungInKim
Copy link
Author

thanks you!

@DanielGoldfarb
Copy link

+1 thanks

@Pringleman83
Copy link

Thank you!

@junetech
Copy link

junetech commented Jan 2, 2019

Thank you!

@pareksha
Copy link

pareksha commented Jan 8, 2019

Nice, thanks!

@maufranchini
Copy link

Thanks!

PS: You've a typo (directroy --> directory)

Thanks!

@ink1
Copy link

ink1 commented Jul 6, 2019

os.makedirs(directory, exist_ok=True)

@TonaIslas-Git
Copy link

os.makedirs(directory, exist_ok=True)

This syntax is actually in vs code intellisense. The second parameter is in case that exists returned true? which should be like if (os.....)

@SebastianoX
Copy link

With pathlib:

from pathlib import Path

path_to_folder = Path("/home/kenny/gist")
path_to_folder.mkdir(exist_ok=True)

@neutrixs
Copy link

neutrixs commented Jun 5, 2021

thx

@jarvissilva937
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment