I am trying to write a Python script that checks if file exists in a folder if yes, prints file available else skips the steps and checks the next folder. The other additional step I do is if there are any sub-folders within the folder I delete the sub-folder. However, if the folder does not exist the code fails. How could I skip the folder if it does not exist.Given below is the code I am using:
import pandas as pd
import glob
import numpy as np
import os
import os, shutil
path = r'/Users/scott/desktop/sales_data/store1/2018-05-10'
for the_file in os.listdir(path):
file_path = os.path.join(path, the_file)
try:
if os.path.isdir(file_path): shutil.rmtree(file_path)
except Exception as e:
print(e)
The error I get is
FileNotFoundError: [Errno 2] No such file or directory: '/Users/scott/desktop/sales_data/store1/2018-05-10'
Could anyone assist. Thanks.