RPA / Robocorp
Files and folders / Remove
Remove a single folder
tasks.py
from robocorp.tasks import task
import shutil
@task
def remove_folder():
path = "my_folder"
shutil.rmtree(path)
Remove multiple folders
tasks.py
from robocorp.tasks import task
import shutil
import os
@task
def remove_folders():
# List of folders to be deleted
folders_to_delete = [
"my_folder1", # Example folder 1
"my_folder2", # Example folder 2
"my_folder3/subfolder" # Example of an absolute path
]
for folder_path in folders_to_delete:
try:
# Check if the folder exists before attempting to delete
if os.path.exists(folder_path):
shutil.rmtree(folder_path)
print(f"Folder '{folder_path}' has been deleted successfully.")
else:
print(f"Folder '{folder_path}' does not exist.")
except Exception as e:
print(f"An error occurred while deleting '{folder_path}': {e}")
Remove a single file
tasks.py
from robocorp.tasks import task
import os
@task
def remove_file():
path = "my_file.txt"
os.remove(path)
Remove multiple files
tasks.py
from robocorp.tasks import task
import os
@task
def remove_files():
# List of files to be deleted
files_to_delete = [
"my_file1.txt", # Example file 1
"my_file2.txt", # Example file 2
"my_folder/my_file3.txt" # Example of an absolute path
]
for file_path in files_to_delete:
try:
# Check if the file exists before attempting to delete
if os.path.exists(file_path):
os.remove(file_path)
print(f"File '{file_path}' has been deleted successfully.")
else:
print(f"File '{file_path}' does not exist.")
except Exception as e:
print(f"An error occurred while deleting '{file_path}': {e}")