Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Directory in python [duplicate]

I want to make a directory in Python.

Here is my code:

dl_path = "~/Downloads/PDMB"

def main():
    if not os.path.exists(dl_path):
        print "path doesn't exist. trying to make"
        os.makedirs(dl_path)

if __name__ == '__main__':
    main()

I want that pdmb be in Download folder in $HOME (by the way my OS is Ubuntu), but it makes Home/Downloads/pdmb in the same folder that my code is.

what should I do?

like image 688
Ali Salehi Avatar asked Dec 23 '22 12:12

Ali Salehi


1 Answers

You need to use expanduser to expand the '~' path

Here's the code you need

import os
from os.path import expanduser

home = expanduser('~')

dl_path = home + '/Downloads/PDMB'

def main():
    if not os.path.exists(dl_path):
       print "path doesn't exist. trying to make"
       os.makedirs(dl_path)



 if __name__ == '__main__':
    main()
like image 105
Akshay Avatar answered Dec 26 '22 02:12

Akshay