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

google Colaboratoryに登録する
google driveをマウントする(/content/drive/My Drive/)
下記のソースを実行する

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
广告
将在 10 秒后关闭
bannerAds