Created
October 31, 2023 18:29
-
-
Save pakkinlau/1045344c394f578b1669c66e15abde7a to your computer and use it in GitHub Desktop.
This Python script facilitates the management of two spaces: a creation space (a large and private area for work) and a publication space (a smaller area for selected items ready for publishing). Users can specify a list of items to migrate from the creation space to the publication space, streamlining the publishing process.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Successfully Migrated:\n", | |
"/home/kin/All_github_repos/textual-notes/Machine learning/Gram-Schmidt projection.md\n", | |
"\n", | |
"Failed Migration:\n" | |
] | |
} | |
], | |
"source": [ | |
"import os\n", | |
"import shutil\n", | |
"\n", | |
"def copy_markdown_files(source_folder, target_folder, markdown_names):\n", | |
" # Expand user paths to handle both Linux and Windows\n", | |
" source_folder = os.path.expanduser(source_folder)\n", | |
" target_folder = os.path.expanduser(target_folder)\n", | |
"\n", | |
" # Create the target folder if it doesn't exist\n", | |
" os.makedirs(target_folder, exist_ok=True)\n", | |
"\n", | |
" successfully_migrated = []\n", | |
" failed_migration = []\n", | |
"\n", | |
" for root, dirs, files in os.walk(source_folder):\n", | |
" for filename in files:\n", | |
" if filename.endswith('.md') and any(name in filename for name in markdown_names):\n", | |
" source_path = os.path.join(root, filename)\n", | |
" target_path = os.path.join(target_folder, filename)\n", | |
" try:\n", | |
" shutil.copy(source_path, target_path)\n", | |
" successfully_migrated.append(source_path)\n", | |
" except Exception as e:\n", | |
" failed_migration.append((source_path, str(e)))\n", | |
"\n", | |
" return successfully_migrated, failed_migration\n", | |
"\n", | |
"\n", | |
"if __name__ == \"__main__\":\n", | |
" source_folder = \"/home/kin/All_github_repos/textual-notes/\"\n", | |
" target_folder = \"/home/kin/All_github_repos/BigdataMath/\"\n", | |
" markdown_names = [\"Gram-Schmidt projection\"] # Replace with the desired markdown file names\n", | |
"\n", | |
" successfully_migrated, failed_migration = copy_markdown_files(source_folder, target_folder, markdown_names)\n", | |
"\n", | |
" print(\"Successfully Migrated:\")\n", | |
" for file in successfully_migrated:\n", | |
" print(file)\n", | |
"\n", | |
" print(\"\\nFailed Migration:\")\n", | |
" for file, error in failed_migration:\n", | |
" print(f\"{file}: {error}\")\n" | |
] | |
} | |
], | |
"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.10.12" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment