I investigated how to operate the following files and folders using the standard python modules os and shutil, so I summarized them.
-How to create a directory -How to copy the entire contents of the directory -How to delete files -How to delete the entire contents of the directory -How to delete an empty directory
You can create a directory with the os.mkdir () function. If the directory already exists, you will get an error.
import os
os.mkdir("fold")
You can use the shutil.copytree () function to copy an existing directory to another directory with its contents.
import shutil
shutil.copytree("fold1","fold2") #Copy fold1 as fold2
You can remove the file with the os.remove () function.
import os
os.remove("file.txt") #"file.txt"Delete
You can delete the entire directory with the shutil.rmtree () function.
import shutil
shutil.rmtree("fold1") #"fold1"Delete the entire contents
You can delete an empty directory with the os.rmdir () function.
import os
os.rmdir("fold1") #"fold1"Delete
Recommended Posts