How to create a folder in Drive using google colab? (using python3 )

 How to create a folder in Drive using google colab

As we have discussed in previous post I would like to keep this post  as the continuation of the previous post , So I would recommend you to go through the previous post once for better understanding

How to install packages/modules in colab? - python

!pip install pydub
from google.colab import drive
import os
from pydub import AudioSegment
from pydub.utils import make_chunks
 
so  we have installed and imported the required modules for our project💥 

Now we have to access our drive from colab.

the following line of code will do that work!
 

Python program:

---------------------------------------------------------------------------------

drive.mount('/content/gdrive')

It will mount our drive into colab .
The final code will be like:
 
!pip install pydub
from google.colab import drive
import os
from pydub import AudioSegment
from pydub.utils import make_chunks

drive.mount('/content/gdrive') 

-------------------------------------------------------------------------------------------------------------------------
 
-- Now run this piece of code in colab 
and it will popup with a text box and a link! we have to authorize the colab for accessing our drive 😌

so just follow the link shown  in output window and login to your account from that link  (if you are already logged in your account you may not need to login again )
 
after authorization you will get a link copy and paste it in the text box shown in the output window of colab.
 
Now we are free to access our drive from colab notebook💥 
 
Next we are going to create a folder in Drive - let say a folder to save my output (chunks of wav files!)
 
  This line of code will do that stuff for us!
 
 

os.mkdir("/content/gdrive/My Drive/Output_folder")

 
/content/gdrive/My Drive/ --> is the location/path in which I want to create new folder
Output_folder --> name for my new folder 
 
  Wait wait!!
  there is a issue with this code !!!
 
Obviously it will run smoothly in your first run (if you don't have folder with the same name(Output_folder) )  

But in the next run we have already created a folder with the name
Output_folder so it will surly gonna throw error message!
 
we can solve this issue by simply using try: and except:
 
so the final code will be like:
 
!pip install pydub

from google.colab import drive

import os
from pydub import AudioSegment
from pydub.utils import make_chunks

drive.mount('/content/gdrive')

try:
    os.mkdir("/content/gdrive/My Drive/Output_folder")
except:
    print("Folder already found")
related posts: