<jupyter_start><jupyter_text>The operating system ![](ghtop_images/header2.png)<jupyter_code>import os
import sys<jupyter_output><empty_output><jupyter_text>name of operating system<jupyter_code>print('os.name',',',os.name,'\n')<jupyter_output>os.name , nt<jupyter_text>get current dir<jupyter_code>print('os.getcwd()',',',os.getcwd(),'\n')<jupyter_output>os.getcwd() , C:\Users\44781\pyproj\_misc<jupyter_text>list files in dir<jupyter_code>print('os.listdir()',',',os.listdir(),'\n')<jupyter_output>os.listdir() , ['.ipynb_checkpoints', 'adapic.jpg', 'awarhol.jpg', 'awarhol.webp', 'dog.jpg', 'draw.png', 'ebsd2.tif', 'ebsdmap.png', 'ebsdmap.tif', 'fastAI_C1_notes.ipynb', 'image_1000.jpg', 'image_5000(1).jpg', 'IndeedExtract-Copy1.ipynb', 'IndeedExtract-Copy2.ipynb', 'No_61_Mark_Rothko-thumbnail_webp-9999x9999.webp', 'os.ipynb', 'output', 'PF_500C R.png', 'PythonBook.ipynb', 're.ipynb', 'styleTransfer.ipynb', 'test2', 'Untitled.ipynb', 'vangogh.jpg', 'vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5', 'water.webp']<jupyter_text>make a directory<jupyter_code>this_dir=os.getcwd()
directory='test'
#this adds either / or \ depending on os
path = os.path.join(this_dir, directory)

try:
    os.mkdir(path)
except:
    pass

#or 
try:
    os.mkdir('test2')
except:
    pass<jupyter_output><empty_output><jupyter_text>list files in dir<jupyter_code>print('os.listdir()',',',os.listdir(),'\n')<jupyter_output>os.listdir() , ['.ipynb_checkpoints', 'adapic.jpg', 'awarhol.jpg', 'awarhol.webp', 'dog.jpg', 'draw.png', 'ebsd2.tif', 'ebsdmap.png', 'ebsdmap.tif', 'fastAI_C1_notes.ipynb', 'image_1000.jpg', 'image_5000(1).jpg', 'IndeedExtract-Copy1.ipynb', 'IndeedExtract-Copy2.ipynb', 'No_61_Mark_Rothko-thumbnail_webp-9999x9999.webp', 'os.ipynb', 'output', 'PF_500C R.png', 'PythonBook.ipynb', 're.ipynb', 'styleTransfer.ipynb', 'test', 'test2', 'Untitled.ipynb', 'vangogh.jpg', 'vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5', 'water.webp']<jupyter_text>Check file/dir exists<jupyter_code>print(os.path.exists("test2"),os.path.exists("test"))<jupyter_output>True True<jupyter_text>change directory<jupyter_code>os.chdir(directory)
print('os.getcwd()',',',os.getcwd(),'\n')<jupyter_output>os.getcwd() , C:\Users\44781\pyproj\_misc\test<jupyter_text>Create and Rename a file<jupyter_code># create a file 'a' is append
open('Old.txt','a').close()

# check file exists 
print('old=',os.path.exists("Old.txt"),'. new=',os.path.exists("New.txt"))

# rename a file
fd = "Old.txt"
os.rename(fd,'New.txt')

# check file exists 
print('old=',os.path.exists("Old.txt"),'. new=',os.path.exists("New.txt"))<jupyter_output>old= True . new= False
old= False . new= True<jupyter_text>Copy a file- can be done in os but easier in shutilhttps://stackabuse.com/how-to-copy-a-file-in-python/<jupyter_code>import shutil
shutil.copyfile('New.txt', 'Old.txt')

# check file exists 
print('old=',os.path.exists("Old.txt"),'. new=',os.path.exists("New.txt"))<jupyter_output>old= True . new= True<jupyter_text>Remove a file<jupyter_code># remove a file
os.remove("New.txt")
# os.remove("Old.txt")

# check file exists 
print('old=',os.path.exists("Old.txt"),'. new=',os.path.exists("New.txt"))<jupyter_output>old= True . new= False<jupyter_text>Go back up in directory<jupyter_code>os.path.dirname(os.path.dirname(   ))
#remove directory
os.rmdir(path)
#list files in dir
print('os.listdir()',',',os.listdir(),'\n')<jupyter_output>os.listdir() , ['.ipynb_checkpoints', 'adapic.jpg', 'awarhol.jpg', 'awarhol.webp', 'dog.jpg', 'draw.png', 'ebsd2.tif', 'ebsdmap.png', 'ebsdmap.tif', 'fastAI_C1_notes.ipynb', 'image_1000.jpg', 'image_5000(1).jpg', 'IndeedExtract-Copy1.ipynb', 'IndeedExtract-Copy2.ipynb', 'No_61_Mark_Rothko-thumbnail_webp-9999x9999.webp', 'os.ipynb', 'output', 'PF_500C R.png', 'PythonBook.ipynb', 're.ipynb', 'styleTransfer.ipynb', 'test2', 'Untitled.ipynb', 'vangogh.jpg', 'vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5', 'water.webp']<jupyter_text>Create new files<jupyter_code>#create new file and edit
nano file.txt
#create file
touch file.txt

!cd<jupyter_output>C:\Users\44781\pyproj\_misc\test<jupyter_text>Subprocess module<jupyter_code>import subprocess

# subprocess.run("date",shell=True)
print(subprocess.run(['cmd', '/c', 'date']))<jupyter_output>CompletedProcess(args=['cmd', '/c', 'date'], returncode=1)
{'fraction_non_alphanumeric': 0.20037062775075284, 'fraction_numerical': 0.02524901552003706, 'mean_word_length': 7.758620689655173, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '62909', 'license': 'Apache-2.0', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1814, 'n_words': 235, 'path': '_notebooks/2021-10-04-OS.ipynb', 'repo_name': 'ThomasHSimm/THS', 'size': '4317'}