-1

I am getting the output of a directory using subprocess like this :-

directory_content = subprocess.getoutput("ssh -i /key.pem ubuntu@IP ls -l --time-style=long-iso /opt/orientdb/databases" | awk -F' ' '{print $6 $8}'")

And i am converting the output to a list of tuples such that i get the directory and the date on which the directory was created.

[('2019-04-25', 'database1'), ('2019-04-26', 'database2') .......]

And i am looping over list and getting the respective directory

for date,db in directory_content:
    if (date == '2019-04-25'):
        os.remove(db)

but i am getting the error message :-

 os.remove(db)
FileNotFoundError: [Errno 2] No such file or directory

I can use some help on how i can delete the directory. Thank you.

Somethingwhatever
  • 1,390
  • 4
  • 32
  • 58

1 Answers1

1

FileNotFoundError: [Errno 2] No such file or directory

That doesn't say you can't delete the file. That says there is no file. The computer is surely telling you truth about that.

I see two problems.

  1. The directory list is fetched from ubuntu@IP, but afaict os.remove is executed locally. That could be a ... problem.
  2. The directory names are relative to /opt/orientdb/databases, but there's no evidence that's the current working directory when the process executes.

For any file not found error in Python that "can't be", a good strategy is to wrap the offending code in a try/catch block and, when the exception is caught, print both the argument name and the current working directory. Nearly always, when you string those two pieces of information together, you'll agree with the computer with a resounding, "duh!".

James K. Lowden
  • 7,574
  • 1
  • 16
  • 31