I am selecting datas from my database, and deleting inactive domains from my filesystem. Sometime, I have domain, which was deactivated twice in database, script will try to delete it also twice, since it's going for each row.
How to fix the issue, when script will find hosting in database, which was on filesystem already deleted, so it will check it's deleted and will continue and not crash on
os.rmdir('/data/sa/' + name)
FileNotFoundError: [Errno 2] No such file or directory: '/data/sa/test3.com'
masir@linux-hjay:~/Documents/python> ls /data/sa/
Here is my code:
cursor = connection.cursor()
sql="SELECT id, name, state FROM domain WHERE state='2'"
cursor.execute(sql)
records = cursor.fetchall()
print("Total number of records for deleting is: ", cursor.rowcount)
print("\nPrinting each domain record")
for row in records:
print("id = ", row["id"], )
print("name = ", row["name"])
print("state = ", row["state"], "\n")
id = row["id"]
name = row["name"]
state = row["state"]
if state == 2:
# print(state)
print('found records for deleting' + name)
os.rmdir('/data/sa/' + name)
else:
print('no records for deleting found')
print('domain', name)
print('hast state', state, "\n")