Last active
July 25, 2023 06:50
-
-
Save hhankj2u/3aa822f5f078c7c59b8fcbe3fad62e06 to your computer and use it in GitHub Desktop.
Python script for https://www.wikidocs.it/
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
# from | |
folder1 | |
file1.md | |
file2.md | |
# to | |
folder1 | |
file1 | |
content.md | |
file2 | |
content.md |
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
import os | |
import shutil | |
def convert_files_to_folders(directory_path): | |
# Get a list of all files and subdirectories in the current directory | |
for entry in os.scandir(directory_path): | |
if entry.is_file() and entry.name.endswith('.md'): | |
# Remove the ".md" extension from the file name to get the folder name | |
folder_name = entry.name[:-3] | |
# Create the folder if it doesn't exist | |
folder_path = os.path.join(directory_path, folder_name) | |
os.makedirs(folder_path, exist_ok=True) | |
# Move the file into the folder and rename it to "content.md" | |
new_file_path = os.path.join(folder_path, 'content.md') | |
shutil.move(entry.path, new_file_path) | |
elif entry.is_dir(): | |
# Recursively process subdirectories | |
convert_files_to_folders(entry.path) | |
if __name__ == "__main__": | |
# Replace 'directory_path' with the path to the directory containing your Markdown files | |
directory_path = '/path/to/your/directory' | |
convert_files_to_folders(directory_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment