Django delete files from a directory not working - python

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.

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('/', '\')))

Related

Unable to open files returned by os.listdir

I have some code like this:
import os
directory = 'H:\\path\\to\\files'
for file in os.listdir( directory ):
open( os.path.join( directory, file ) )
In a very small number of cases, running this code on a directory will throw the following error.
IOError: [Errno 2] No such file or directory: 'H:\\path\\to\\files\\blahblah.file'
This doesn't happen for every file in the directory - the first few are opened correctly, but then it will fail on one of the files, the same each time.
The files in question do exist, so it's a problem with open rather than with os.listdir.
Strangely, changing the working directory and changing directory to be the corresponding relative path makes a difference to which file we fail on.
I am running Python 2.7 on Windows 10.
Update It seems to be connected to the length of the file names. The file names are very long in general: if I rename the file to something shorter, then it succeeds; longer and it fails again. That might explain why we were failing at different points when using a longer relative path.
This seems to have been caused by the 260-character path length limit. The files that were failing had paths over 260 characters long.

How to resolve the file path too long issue on windows by using Python 3? (tried other methods on this platform)

I was using the code shutil.copyfile(src_file, dst_file) to copy the source file into the target destination. However, the path is apparently too long, so I got the error 'FileNotFoundError: [Errno 2] No such file or directory: .\\Merged\\Book\\Book123\\New\\library\\helloworld\\ass\\data\\explore_the_world\\explore_content\\sassy_pane\\conditional_testing\\formatting_life_session\\locales\\en\\conditional_fformatting_life_session_component_new.strings.json'
I think the double slash was built into the Python file path interpretation, so I can't just change it from my script.. I've read the method of adding u(\\\\?\) to the file path, such as shutil.copyfile(u"\\\\?\\" + src_file, u"\\\\?\\" + dst_file), but it doesn't work.. I'll get an error of 'FileNotFoundError: [Errno 2] No such file or directory: \\\\?\\.\\Merged\\Book\\Book123\\New\\library\\helloworld\\ass\\data\\explore_the_world\\explore_content\\sassy_pane\\conditional_testing\\formatting_life_session\\locales\\en\\conditional_fformatting_life_session_component_new.strings.json'
I've read this article, but it doesn't really help solve my issue..Could anybody enlighten me over this? Thank you!!
Python: copy long file path Shutil.copyfile

Pathlib can't find a file on Ubuntu

Trying to access a CSV file in the following directory with a systemd service: /path/to/cwd/data/x.csv
Currently, hard-coding the path in the python file in my CWD with path = '/path/to/cwd/data/x.csv' allows the service to find the file without issues.
However, if I try to use pathlib like so:
from pathlib import Path
path = Path.cwd() / 'data' / 'x.csv'
The service gives me an error:
FileNotFoundError: [Errno 2] No such file or directory: '/data/x.csv'
I get the same error when I try to use the OS library to do path = os.path.join(os.getcwd(), 'data/x.csv')
I have no idea what's going on, when I compare the output of the paths generated by pathlib and os, they're exactly the same as what I type out, but they don't work and the hardcoded path does.
You can try to specify the cwd inside the service.
[Service]
WorkingDirectory=/PathToCwd
Then run it again.

Move files from trash to folder in python using Mac OS

trash = "~/.Trash"
new_folder = []
for current_file in os.listdir(trash):
new_folder.append(current_file)
The above code is not working. I am trying to move files from the trash to a folder in python on a Mac. The error message I'm getting is
FileNotFoundError: [Errno 2] No such file or directory: '~/.Trash'
Expanding the ~ is a feature of your shell. Your can not use it like a normal file system path. Luckily Python has a function to assist you with that:
trash = os.path.expanduser('~/.Trash')
See https://docs.python.org/3/library/os.path.html#os.path.expanduser for details.

How do i overcome a permissions error

Hello i am trying to delete a directory which is like a temporary file storage. however it does not work and keeps throwing the same erros
directory = ("C:\\Users\\Bradley\\Desktop\\Log in system\\TempFiles")
os.remove(directory)
here is the error:
PermissionError: [WinError 5] Access is denied:
'C:\\Users\\Bradley\\Desktop\\Log in system\\TempFiles'
Check your permissions
os.remove requires a file path, and raises OSError if path is a
directory. If path is a directory, OSError is raised; see rmdir()
below to remove a directory.
Try this:
os.rmdir("C:\\Users\\Bradley\\Desktop\\Log in system\\TempFiles")
In other way you can use this trick ;) :
import subprocess
subprocess.call(['runas', '/user:Administrator', 'Your command'])
And, according to this post, you can run your program as an administrator by right click and run as administrator.

Categories

Resources