Rejoanul Alam
Rejoanul Alam

Reputation: 5398

Django delete files from a directory not working

The following is my file deletion code:

path = settings.MEDIA_ROOT
os.remove(os.path.join(path, file_name))

When run I get the following HTTP 500 error:

WindowsError at /project/delete_files/

[Error 2] The system cannot find the file specified: u'C:\DjangoEmt/static/uploads/bridge.jpg'

I have checked the file exists in the directory. Can someone please help me as to why its not working.

Sidenote: I am using ajax in django if this makes any difference.

Upvotes: 0

Views: 1379

Answers (1)

Alexandr Faizullin
Alexandr Faizullin

Reputation: 66

I see in your file path error, a combination of slash and backslash. replace '/' to '\' if your application run on windows system.

path = settings.MEDIA_ROOT
os.remove(os.path.join(path, file_name.replace('/', '\')))

Upvotes: 1

Related Questions