0

i am running a flask project and i am looking for a way to create a directory ABOVE the path from which the current App is running. For example:

dirA -->
    dirBinA -->
        peter.py
        griffin.sh
    dirCinA -->
        index.py <--------- this is the flask app that's running
        tom.css
        dick.html
        harry.js
    dirDinA -->  <--------- this directory doesn't exist yet
        anotherDir -->
            turtle.py

i want to create a new directory anotherDir inside a new directory dirDinA from the flask app that's running in dirCinA/index.py

If I try with os.mkdir("../dirDinA/anotherDir/"), then flask says OSError: [Errno 2] No such file or directory: '../dirDinA/anotherDir'

Rakib
  • 12,376
  • 16
  • 77
  • 113
  • The current directory is not the directory of the file that is executing. Check http://stackoverflow.com/questions/2632199/how-do-i-get-the-path-of-the-current-executed-file-in-python – korylprince May 31 '13 at 23:56
  • Why would you need to do this? – Blender May 31 '13 at 23:57
  • @user2246674, you are correct... the starting '/' was a typo from my end... very sorry for that.. i am correcting the question – Rakib Jun 01 '13 at 01:36
  • @syedrakib Might find useful information in http://stackoverflow.com/questions/918154/relative-paths-in-python – user2246674 Jun 01 '13 at 09:02
  • i must apologize to everyone... while writing the question i ignored some of the original cases for which the error was actually being produced... i have corrected the question appropriately... please accept my apologies for the mis-understandings created... thanks to everyone for the helpful comments posted here – Rakib Jun 02 '13 at 04:41

4 Answers4

4

You can use os.makedirs to create multiple directory levels in a single call:

os.makedirs("../dirDinA/anotherDir")
Jeff Bauer
  • 13,890
  • 9
  • 51
  • 73
1

Assuming that you're running the Python script in the intended directory, you need to create a new directory and put things in it in two steps. For instance, if ../dirDinA does not exist, then

os.mkdir("../dirDinA/a_new_file.py")

produces the "No such file or directory" error (and misleadingly shows you the full path that you want to make, not the part whose non-existence is causing the problem). The following

os.mkdir("../dirDinA")
open("../dirDinA/a_new_file.py", "w").write("test")

does not produce an error.

Jim Pivarski
  • 5,568
  • 2
  • 35
  • 47
  • Thanks @Jim Pivarski... your answer was helpful for my problem... i have corrected my question a little bit and, hence, i have edited this answer as well. Thanks again. – Rakib Jun 02 '13 at 04:43
1

In order to create a new 2-level-depth directory, you need to create it in TWO steps. For instance, if ../dirDinA doesn't yet exist, then the following command fails.

os.mkdir("../dirDinA/anotherDir")

It produces the OSError: No such file or directory, misleadingly, showing you the FULL path that you are trying to create, instead of highlighting on the ACTUAL part whose non-existence is producing the error.

However, the following 2 step method goes well without any error

os.mkdir("../dirDinA")
os.mkdir("../dirDinA/anotherDir")

Directory ../dirDinA needs to exist before anotherDir can be created inside it

Thanks goes to the answer by @JimPivarski.

Community
  • 1
  • 1
Rakib
  • 12,376
  • 16
  • 77
  • 113
-1

I suggest you use os.popen:

os.popen("cd ..; mkdir dirDinA; mkdir ./dirDinA/anotherDir; --code for creating turtle.py--; chmod +x turtle.py; python turtle.py");

Hope it helpful for you.

openxxs
  • 146
  • 3