{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Files & Directories" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## An example of using the OS function to create a directory and move a file:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "# Create a directory and move a file from one directory to another\n", "# using low-level OS functions.\n", "\n", "import os\n", "\n", "# Check to see if a directory named \"test1\" exists under the current\n", "# directory. If not, create it:\n", "dest_dir = os.path.join(os.getcwd(), \"test1\")\n", "if not os.path.exists(dest_dir):\n", " os.mkdir(dest_dir)\n", "\n", "\n", "# Construct source and destination paths:\n", "src_file = os.path.join(os.getcwd(), \"sample_data\", \"README.md\")\n", "dest_file = os.path.join(os.getcwd(), \"test1\", \"README.md\")\n", "\n", "\n", "# Move the file from its original location to the destination:\n", "os.rename(src_file, dest_file)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Here is an example of using Pathlib to create a directory and move a file:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create a directory and move a file from one directory to another\n", "# using Pathlib.\n", "\n", "from pathlib import Path\n", "\n", "# Check to see if the \"test1\" subdirectory exists. If not, create it:\n", "dest_dir = Path(\"./test1/\")\n", "if not dest_dir.exists():\n", " dest_dir.mkdir()\n", "\n", "# Construct source and destination paths:\n", "src_file = Path(\"./sample_data/README.md\")\n", "dest_file = dest_dir / \"README.md\"\n", "\n", "# Move the file from its original location to the destination:\n", "src_file.rename(dest_file)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The OS module \n", "Python’s OS module, or the miscellaneous operating system interface, is very useful for file operations, directories, and permissions. Let’s take a look at each.\n", "\n", "## File operations\n", "File names can be thought of as two names separated by a dot. For example, helloworld.txt is the file name and the extension defines the file type. OS provides functions to create, read, update, and delete files. Some of the basic functions include:\n", "\n", "## Opening and closing files\n", "\n", "Reading from and writing to files\n", "\n", "Appending to files\n", "\n", "## Directories\n", "OS also provides functions to create, read, update, and delete directories, as well as change directories and list files. Knowing how to use these functions is key to working with files. For example, os.listdir( path ) returns a list of all files and subdirectories in a directory.\n", "\n", "## Permissions\n", "Having the ability to update file permissions is an important aspect of making installations from a terminal window. The os.chmod() provides the ability to create, read, and update permissions for individuals or groups.\n", "\n", "## Things to keep in mind \n", "One thing to be aware of is that Python treats text and binary files differently. Because Python is cross-platform, it tries to automatically handle different ASCII line endings. If you’re processing a binary file, make sure to open it in binary mode so Python doesn’t try to “fix” newlines in a binary file.\n", "\n", "A best practice is to always close() a file when you’re done reading or writing to it. Even though Python usually closes them for you, it’s a good signal to other people reading your code that you’re done with that file. Make sure to catch any potential errors from filesystem calls, such as permission denied, file not found, and so on. Generally, you wrap them in try/except to handle those errors.\n", "\n", "## Key takeaways\n", "There are several ways to manage files and directories in Python. One way is to use low-level functions in the OS and SYS modules that closely mimic standard Linux commands. Another way is to utilize the Pathlib module, which provides an object-oriented interface to working with the file systems. \n", "\n", "## Resources for more information\n", "More information about files and directories can be found in several resources provided below: \n", "- https://docs.python.org/3/library/os.html\n", "- https://docs.python.org/3/library/os.path.html\n", "- https://en.wikipedia.org/wiki/Unix_time" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 2 }