Folder creation / file move / compress / delete operations with python

Create folder makedirs

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')

Create a file to operate once

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)

Get the file name in the folder

import glob
files = glob.glob(dir_path+"\\"+csv_name)

Move files

import shutil
move_csv_path = shutil.move(files[0], dir_path_sub_folder+"\\"+csv_name)

Compress to zip

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)

Delete file

os.remove(move_csv_path)

Decompress the compressed file

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')

Delete folders and files

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)

that's all

Recommended Posts

Folder creation / file move / compress / delete operations with python
Python memo ① Folder and file operations
File operations in Python
File operations with open — "../"
File operations in Python
Summary of python file operations
Draw netCDF file with python
Easy folder synchronization with Python
[Python] Folder monitoring with watchdog
Download csv file with python
[Python] Create a file & folder path specification screen with tkinter
Extract the xz file with python
File / folder path manipulation in Python
[Python] Write to csv file with Python
[Automation with python! ] Part 1: Setting file
Implemented file download with Python + Bottle
Linebot creation & file sharing in Python
Output to csv file with Python
Create an Excel file with Python3
Move data to LDAP with python Change / Delete (Writer and Reader)
ORC, Parquet file operations in Python
A memo organized by renaming the file names in the folder with python
Read the file with python and delete the line breaks [Notes on reading the file]
Creating a simple PowerPoint file with Python
Exclusive control with lock file in Python
Read CSV file with python (Download & parse CSV file)
Check the existence of the file with python
Quickly create an excel file with Python #python
Let's read the RINEX file with Python ①
See file and folder information on python
Create Excel file with Python + similarity matrix
Record with Python → Save file (sounddevice + wave)
Organize data divided by folder with Python
WebApi creation with Python (CRUD creation) For beginners
Excel table creation with Python [Progress management table]
I made a configuration file with Python
[Automation] Read mail (msg file) with Python
[Python3] Understand the basics of file operations
How to drop Google Docs in one folder in a .txt file with python
File creation
File operations
I tried searching for files under the folder with Python by file name
Split mol2 file with python (-> 2016.04.17 Also supports sdf file)
nginxparser: Try parsing nginx config file with Python
How to read a CSV file with Python 2/3
[Python] Get the files in a folder with Python
[Python] How to read excel file with pandas
Convert svg file to png / ico with Python
Read table data in PDF file with Python
Move THORLABS automatic stage with Python [for research]
Develop Windows apps with Python 3 + Tkinter (exe file)
Recent ranking creation using Qiita API with Python
Extract zip with Python (Japanese file name support)
Archive and compress the entire directory with python
Create a Photoshop format file (.psd) with python
Simple sales tool creation with Python GUI: Quote creation
Convert the character code of the file with Python3
Wav file generation from numeric text with python
Read line by line from a file with Python
I want to write to a file with Python
Open a file dialog with a python GUI (tkinter.filedialog)