[PYTHON] Jupyter study notes_001

How to create a folder on google drive in Python:

  1. Register at google Colaboratory
  2. Mount google drive (/ content / drive / My Drive /)
  3. Run the source below

crt_folder.py


import os
import subprocess
import re
import shutil

def res_cmd_lfeed(cmd):
  return subprocess.Popen(
      cmd, stdout=subprocess.PIPE,
      shell=True).stdout.readlines()

def main():
    #Specify the initial path
    mount_path = '/content/drive/My Drive/'
    base_folder = 'python_list/'
    path_name = mount_path  + base_folder

    if not os.path.exists(path_name):
        os.mkdir(path_name)
    #else:
        ####Delete subfolders and files
        #shutil.rmtree(path_name) 
        ####It also recursively deletes subfolders. However, if there are files in the folder, the deletion will fail
        #os.removedirs(path_name) 

    #Output command to list
    cmd = ("pip freeze")
    folders = res_cmd_lfeed(cmd)

    #Read the contents of the list in a FOR loop
    for folder in folders:
        result = str(folder).replace(".","_")
        folder_name = result[2:len(result)-3]
        path_name = mount_path + base_folder + folder_name
        #Create if the folder does not exist
        if not os.path.exists(path_name):
            os.mkdir(path_name)
            ###Uncomment and run if necessary
        #else:
            #shutil.rmtree(path_name)
            #os.removedirs(path_name)

if __name__ == '__main__':
    main()

--Results of implementation creat folder

Recommended Posts

Jupyter study notes_006
Jupyter study notes_008
Jupyter study notes_004
Jupyter study notes_001
Python study notes _000
Python study notes_006
Linux study notes
Python study notes _005
Python study notes_001
Jupyter Study Note_003
Jupyter Study Note_007
Jupyter Study Note_005
Jupyter Official DockerHub Notes
New Linux textbook (study notes)
JetBrains_Learning Notes_003
Jupyter begins
Jupyter Tips 4
SQLAlchemy notes
pyenv notes
Jupyter nbextensions
Jupyter Tips 5
Jupyter, numpy, matplotlib notes used in reports
Jupyter Tips 3
Pandas notes
Sphinx notes
django notes
ipython + jupyter + plotly (matplotlib) settings & usage notes
Jupyter installation
Jupyter tricks
Django notes