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
Recommended Posts