Skip to content

Instantly share code, notes, and snippets.

@teroyks
Last active January 12, 2021 17:20
Show Gist options
  • Save teroyks/eb04baf99d8a921af4f29c51b1b2f347 to your computer and use it in GitHub Desktop.
Save teroyks/eb04baf99d8a921af4f29c51b1b2f347 to your computer and use it in GitHub Desktop.
Pathlib Essentials
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "# File Paths with pathlib\n\nRequires Python ≥ 3.4"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "from pathlib import Path",
"execution_count": 8,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "fullpath = Path('/foo/bar/baz.ext')",
"execution_count": 9,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Filename without directory path"
},
{
"metadata": {
"trusted": true,
"scrolled": true
},
"cell_type": "code",
"source": "print(fullpath.name)",
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"text": "baz.ext\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Filename without directory path and extension"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(fullpath.stem)",
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": "baz\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "File extension (including the dot separator)"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(fullpath.suffix)",
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
"text": ".ext\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Directory path to file"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(fullpath.parent)",
"execution_count": 13,
"outputs": [
{
"output_type": "stream",
"text": "/foo/bar\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Test if file exists"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(fullpath.exists())",
"execution_count": 14,
"outputs": [
{
"output_type": "stream",
"text": "False\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Create path to file"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(Path('/foo/bar') / 'baz.ext')",
"execution_count": 15,
"outputs": [
{
"output_type": "stream",
"text": "/foo/bar/baz.ext\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Safe for empty directory paths"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "dir = Path('')\nprint(dir / 'foo.sh')",
"execution_count": 16,
"outputs": [
{
"output_type": "stream",
"text": "foo.sh\n",
"name": "stdout"
}
]
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.11",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment