{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Operating system calls\n", "\n", "It's possible to use python to perform command line calls\n", "\n", "We can do this with the os module" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## name of operating system\n", "\n", "We can get the operating system we're working on\n", "\n", "1. nt is windows\n", "2. posix is all unix based systems (Mac, Linux, ...)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(os.name)\n", "\n", "my_os = os.name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## working directory" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "os.getcwd()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(os.getcwd())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## contents of directory" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "os.listdir('.')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "os.listdir('C:\\\\Users\\\\Dan\\\\Google Drive\\\\python course')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## make and rename directory" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "os.mkdir('C:\\\\Users\\\\Dan\\\\Google Drive\\\\python course\\\\new_dir')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "os.rename('C:\\\\Users\\\\Dan\\\\Google Drive\\\\python course\\\\new_dir','C:\\\\Users\\\\Dan\\\\Google Drive\\\\python course\\\\new_dir2')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## create and edit file" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "file = open('C:\\\\Users\\\\Dan\\\\Google Drive\\\\python course\\\\new_dir2\\\\myTXTfile.txt','w')\n", "file.write('hello\\n')\n", "file.write('world!')\n", "file.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }