<jupyter_start><jupyter_text>Files and Folder using Python  We'll use the ```os module``` and ```pathlib``` to create, navigate and delete files and folders. You already know how to do this with terminal commands like ```ls```, ```cd``` and ```mkdir```, but we do it within our script.<jupyter_code>## import libraries
import os  ## allows you to navigate, create, delete folders
from pathlib import Path ## allows to create paths to files and folders<jupyter_output><empty_output><jupyter_text>List what is in the directory NOTE: regular terminal commands have to be in empty cells<jupyter_code>ls
## Python scriptable
os.listdir()
## create a path to folder called some_new_folder
## we store that path in a variable called my_new_directory
my_new_directory = Path('some_new_folder/')
## create that directory
my_new_directory.mkdir(exist_ok=True)
## show directory now
os.listdir()<jupyter_output><empty_output><jupyter_text>You don't have to create a variable for the path, but it is easier to resuse that path```Path('some_new_folder/').mkdir(exist_ok=True)```<jupyter_code>## remove an empty directory
## NOTRE: This only removes empty directories
my_new_directory.rmdir()
## show directory now
os.listdir(".")<jupyter_output><empty_output><jupyter_text>list using terminal command<jupyter_code>ls<jupyter_output><empty_output><jupyter_text>Let's add some crap to some_new_folders and then delete<jupyter_code>## create that directory
my_new_directory.mkdir(exist_ok=True)<jupyter_output><empty_output><jupyter_text>Take a moment to manually add some crap<jupyter_code>## navigate into a directory
os.chdir("some_new_folder/")

ls<jupyter_output><empty_output><jupyter_text>back out of directory<jupyter_code>os.chdir("../")
ls
## To empty a directory with files in it, we use another library called shutil

import shutil
## Now delete all contents
shutil.rmtree(my_new_directory)
ls<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10057321521625846, 'fraction_numerical': 0.0, 'mean_word_length': 7.3478260869565215, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 1, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '227735', 'license': 'MIT', 'n_tokens_mistral': 613, 'n_tokens_neox': 599, 'n_words': 195, 'path': 'week_07/week-7-os_SOLUTION.ipynb', 'repo_name': 'sandeepmj/fall20-student-practical-python', 'size': '1919'}