0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Jupyter学習ノート_001

Last updated at Posted at 2020-01-09

Pythonでgoogle driveにフォルダーを作成する方法:

  1. google Colaboratoryに登録する  
  2. google driveをマウントする(/content/drive/My Drive/)
  3. 下記のソースを実行する
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():
    #初期パスを指定する
    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:
        ####サブフォルダーやファイルを削除する
        #shutil.rmtree(path_name) 
        ####サブフォルダーも再帰的に削除してくれます。ただし、フォルダー内にファイルがあると削除に失敗します
        #os.removedirs(path_name) 

    #コマンドをリストに出力する
    cmd = ("pip freeze")
    folders = res_cmd_lfeed(cmd)

    #FORループでリストの内容を読込み
    for folder in folders:
        result = str(folder).replace(".","_")
        folder_name = result[2:len(result)-3]
        path_name = mount_path + base_folder + folder_name
        #フォルダーが存在しない場合作成する
        if not os.path.exists(path_name):
            os.mkdir(path_name)
            ###必要あればコメントを外して実行する
        #else:
            #shutil.rmtree(path_name)
            #os.removedirs(path_name)

if __name__ == '__main__':
    main()
  • 実施した結果
    creat folder
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?