Forums

Cannot delete file from flask app

Hi there!

I have a Flask app which uses os.remove to delete individual files:

os.remove("/home/username/folder/filename.extension")

it doesn't throw an error, but doesn't delete the file either.

I have also tried moving the file using:

os.rename("/home/username/folder/filename.extension", "/home/username/new_folder/filename.extension")

which basically clones the file to the new path without deleting the old one.

I thought it may have to do with permissions, so I tried:

chmod a+rwx /home/username/folder/

any ideas?

That sounds really odd, I can't think of any way that could fail silently. Could you give a real example of a file that's not being deleted like that, and the exact code you're using? If that would contain information that you don't want to share publicly here on the forums, you can let us know privately using the "Send feedback" link at the top of the page.

The code I'm using is the following, which instead of moving the file, it copies it.

    def archive(self, archive_folder, teams_folder):
        self.save(teams_folder)
        os.rename(
            os.path.join(teams_folder, self.user_id + ".json"),
            os.path.join(archive_folder, self.user_id + ".json"),
        )

I also tried os.remove which doesn't work either:

    def archive(self, archive_folder, teams_folder):
        self.save(archive_folder)
        os.remove(os.path.join(teams_folder, self.user_id + ".json"))

The files I'm trying to move/delete are located in /home/username/app_name/db/teams/.

If I run these commands from terminal, rather than in the Flask app, they work fine, so I'm guessing it's something related to the app's permissions?

I doubt it's specifically permissions -- the app runs as the same user, with the same permissions, as your bash consoles.

What is the value of teams_folder in that code? Is it possible that it's not an absolute path -- that is, you're just using db/teams/ instead of /home/username/app_name/db/teams/?

Yes, it is an absolute path. I'm guessing that if it weren't, and the file couldn't be found, it would throw an error. Moreover, os.rename does copy the file, so the paths are working. It just doesn't delete the old file as it's supposed to do!

Actually, nevermind!

os.rename and os.remove work fine in my Flask app, but there was a tricky artefact in my code.

Thanks for the quick replies.

OK -- glad you worked it out!

with open(file, 'w') as file1: pass

maybe this can work!!

Same problem

@redeyesjoker2020 What is your problem? What about the solutions mentioned above?

In flask app I'm trying to delete a image file with os.remove() but doesn't work fine

When run the command from terminal it works fine

with open(file, 'w') as file1: pass -> this solution not delete image, onlz save image with 0 bytes size

thanks for reply

My code:

absolute_path = os.path.join(os.getcwd(), 'static')
for file in os.listdir(absolute_path):
    if file.endswith('.jpg'):
        os.remove(file)

What happens when you use os.remove? Do you get an error?

I see you've just edited to add the code, thanks for that.

Your code to calculate the absolute path is incorrect; os.getcwd returns the current working directory of the process, not the location of the script. If you're trying to build an absolute path that is the location of the script, then the subdirectory static, you should do this:

absolute_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "static")

I've changed the absolute_path building to:

absolute_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "static")

but got the same error

FileNotFoundError: [Errno 2] No such file or directory: 'b4a99e6876954484b382269d6143b8be.jpg'

The file really exists in "static" folder

thanks

You also need to provide a full path when deleting the file:

os.remove(os.path.join(absolute_path, file))

I'd also recommend renaming your variable file to something like filename, as file is a standard class in Python.

I have made the changes indicated and it works correctly.

Thank you very much

Glad to see that you made it work