FileIO

JSON

To dump data to json file:

output_path = <some_dir> / "some_file.json"
params = {'a': 1, 'b': 2}
with output_path.open('w') as f:
    json.dump(params, f, indent=4)  # `indent` writes each new input on a new line

to load from json file:

with input_path.open() as f:
    result = json.load(f)

shutil

shutil.copy(src_path, dst_path)
from shutil import copyfile, rmtree, move

CSV

import csv
def read_csv_file_into_dict(filename):
    with open(filename, newline='') as csvfile:
        reader = csv.reader(csvfile, delimiter=',')
        x = {row[0]: row[1] for row in reader}
    return x

Readline and Readlines

with open(f, "r") as f:
    a = f.readlines()

Find and delete files/folders

from pathlib import Path
a = list(Path('path_to_the_folder').glob('**/some_file_name'))
for x in a:
    x.unlink()

To delete a folder use shutil.rmtree:

import shutil
shutil.rmtree(Path("a_directory"))

Download URL file into DataFrame

One can do it directly from pandas:

import pandas as pd

# URL of the iris dataset
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'

# Read the iris dataset and store it in a DataFrame
iris_df = pd.read_csv(url, header=None, names=['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class'])

# Display the first 5 rows of the iris DataFrame
print(iris_df.head())

or using requests to download file first:

response = requests.get(url)

if response.status_code == 200:
    with open("iris.csv", "wb") as f:
        f.write(response.content)
else:
    print("Failed to download dataset")