![見出し画像](https://assets.st-note.com/production/uploads/images/129425499/rectangle_large_type_2_bda882f369a17ab06baac3a74057460d.png?width=1200)
Python、「階層をまたぐデータ移動」試してみた。
import os
import shutil
def move_to_top_folder(source_folder):
# Get the path of "移動.txt"
file_path = os.path.join(source_folder, "2", "移動.txt")
# Specify the destination folder (parent folder of "2")
destination_folder = os.path.join(source_folder, "2", "..")
# Move the file to the destination folder
shutil.move(file_path, destination_folder)
def change_permissions(folder_path):
for root, dirs, files in os.walk(folder_path):
for item in dirs + files:
item_path = os.path.join(root, item)
# Change permissions
os.chmod(item_path, 0o777) # 0o777 for read, write, execute permissions
if __name__ == "__main__":
target_folder = "C:\\Users\\OneDrive\\デスクトップ\\d"
print(f"Current working directory: {os.getcwd()}")
# Change permissions
change_permissions(target_folder)
# Call the move_to_top_folder function
move_to_top_folder(target_folder)
print(f"Moved '移動.txt' to the top level of the folder.")
![](https://assets.st-note.com/img/1706735901114-v1nyNp6jZU.png)
![](https://assets.st-note.com/img/1706735934535-mIewQDTSHJ.png)
この記事が気に入ったらサポートをしてみませんか?