'Python: how to create subdirectory with each have the same file at the same time?
import os
# Main working path
os.chdir(r"C:\Python DSA\LeetCode")
main_path = r"C:\Python DSA\LeetCode"
leetcode_subdir_path = []
leetcode_note = []
#
for i in range(3, 8):
leetcode_subdir_path.append(f"Leetcode.00{i}")
leetcode_note.append(f"Leetcode00{i}.md")
#print(leetcode_subdir_path)
for folder_name in leetcode_subdir_path:
temp_folder_path = os.path.join(main_path,folder_name)
# os.mkdir(temp_folder_path)
subdirectories = os.mkdir(temp_folder_path)
with open (file="temp.md", mode="w+") as file:
file.write('Hello world')
main
|_folder001
|_folder002
|_folder003
|_folder004
|_MD.md
I want to create folders within range from 2 to 50 under a main folder, and each of the sub-folder containing an MD according to their folder id, but after running the code,sub-folders have been created,but there is only 1 temp.md created, and it located as below,
main
|_folder001
|_001.md
|_folder002
|_002.md
|_folder003
|_003.md
|_folder004
|_004.md
----------
Questions:
how to rewrite the codes above so that each sub-folder containing an MD file, instead of parallel to the sub-folder?
how can I rewrite the code so that the names of the MD file created has the same number as their located folder? e.g.:
folder name:leetcode011 ==> MD name: leetcode011.md
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
