If the folder exists with exist_ok, it will not be created. Error if not specified.
import os
dir_path = os.path.join('C:\\Users\\username\\Desktop','00')
os.chdir(dir_path)
os.makedirs('new_made', exist_ok=True)
dir_path_sub_folder = os.path.join(dir_path,'new_made')
from sklearn.datasets import load_iris
import pandas as pd
iris = load_iris()
irisDF = pd.DataFrame(iris.data)
csv_name='iris.csv'
irisDF.to_csv(csv_name,header=False,index=False)
import glob
files = glob.glob(dir_path+"\\"+csv_name)
import shutil
move_csv_path = shutil.move(files[0], dir_path_sub_folder+"\\"+csv_name)
import zipfile
zip_name=dir_path_sub_folder+'\\iris.zip'
with zipfile.ZipFile(zip_name, 'w', compression=zipfile.ZIP_DEFLATED) as new_zip:
new_zip.write(move_csv_path, arcname=csv_name)
os.remove(move_csv_path)
zip_file = zipfile.ZipFile(zip_name)
zip_file.extractall()
Specify the decompression destination
os.makedirs('extract_zip', exist_ok=True)
zip_file.extractall('extract_zip')
The contents folder cannot be deleted with remove
os.remove(dir_path_sub_folder)
PermissionError: [WinError 5]Access denied.
Can be completely removed using the shell utility (shutil)
shutil.rmtree(dir_path_sub_folder)
It's not perfect and can be sent to the trash for restoration.
import send2trash
send2trash.send2trash(dir_path_sub_folder)
Recommended Posts