Instead of '+' it is better to use os.path.join
- it works with filenames in a smarter way. Also, you have forgotten to elaborate on the else
statement, when you do the actual job instead of throwing an exception. Also, you need a new name for each new directory - they can't all have the same name. Also, do you want nested directories or directories at the same level?
This makes directories at the same level.
import os
from os.path import *
home = expanduser('~')
dl_path = os.path.join(home, 'Downloads')
def main():
if not os.path.exist(dl_path):
print("path does'nt exist")
else:
a = 1
while a <= 50:
os.makedirs(os.path.join(dl_path, str(a)))
a += 1
if __name__ == '__main__':
main()
This makes nested directories:
import os
from os.path import *
home = expanduser('~')
dl_path = home
def main():
if not os.path.exist(dl_path):
print("path does'nt exist")
else:
a = 1
while a <= 50:
dl_path = os.makedirs(os.path.join(dl_path, 'Downloads'))
a += 1
os.makedirs(dl_path)
if __name__ == '__main__':
main()