Skip to content
Advertisement

Creating directories with OS module

Hi there I’m trying to create a small tool that will create child directory’s within the Root directory

Root Dir(python) And child Dir(DS, WEB, Flask, Learn) stuff like that

At starting I have done it statically Here is the Code

JavaScript

Then I decided to do it dynamically By using two lists just like before but thing’s don’t go well Here is the code

JavaScript

So I do Some try on it by using nested for loop or while loop but I didn’t get an answer for what I did at the second code, I won’t be able to give for k in range(0, A): if I give it’s throwing me Error A subdirectory or file py already exists. And it is obvious to Get the Error like I’m recreating the same content, again and again, using for loop

What I want is to create Root:-Python child’s:-Web, DS..etc

You don’t want to use the same code if you know other ways Please Do let me Know

Please, anyone, Help me

Advertisement

Answer

To prompt the user, you can create a small function which ask for the user to enter values in order.

For example:

JavaScript

This function will loop indefinitely until the user press <ENTER>, and then return the values.

You can then use this function twice: for the directories and for the languages:

JavaScript

Then, you need to loop over the directories and the languages to create the tree structure. Don’t forget to specify the full path of the root directory in a global variable (for instance):

NOTE: in this demonstration, I choose the current working directory: “.”

Solution 1: using os module:

JavaScript

Solution 2: using pathlib module

JavaScript
Advertisement