4

I'm trying to list files using webdab but I'm having issues. I can create directories and put files just fine but not list a directory or pull a file. I'm seeing the error, "Method not supported".

from webdav3.client import Client
options = {
  'webdav_hostname': "https://___________.com/remote.php/dav/files/",
  'webdav_login': "user_name",
  'webdav_password': "password"
}
client = Client(options)
print(client.list('/'))

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/<user>/.local/lib/python3.10/site-packages/webdav3/client.py", line 67, in _wrapper
    res = fn(self, *args, **kw)
  File "/home/<user>/.local/lib/python3.10/site-packages/webdav3/client.py", line 264, in list
    response = self.execute_request(action='list', path=directory_urn.quote())
  File "/home/<user>/.local/lib/python3.10/site-packages/webdav3/client.py", line 228, in execute_request
    raise MethodNotSupported(name=action, server=self.webdav.hostname)
webdav3.exceptions.MethodNotSupported: Method 'list' not supported for https://________.com/remote.php/dav/files
J'e
  • 3,014
  • 4
  • 31
  • 55

3 Answers3

2

The client.list() method assumes the remote root directory by default.
As you supply https://___________.com/remote.php/dav/files/ as your webdav_hostname the root directory it tries to access when you call client.list('/') is the top level files directory. As a Nextcloud user you don't have access to that level, so listing that is impossible. However, you do have access to the files/<username> directory, so listing client.list('/<username>/') works.

To prevent that you have prepend the username to every list command you can set the webdav_hostname to .../remote.php/dav/files/<username>. Then a call to client.list() should work straight away.

Saaru Lindestøkke
  • 2,067
  • 1
  • 25
  • 51
1

Despite already providing a username in the webdav_login, a username is still needed in the call to listdir. For example, to get a list of files in the root directory of the user "user_name", client.list('/user_name/').

J'e
  • 3,014
  • 4
  • 31
  • 55
  • 1
    I can't suggest edits now, so therefore a comment. The solution seems right, but it's not because you need to provide a username (the [`list`](https://github.com/ezhov-evgeny/webdav-client-python-3/blob/develop/webdav3/client.py#L245) method does not have a username argument) it's because you need to provide the root folder that needs to be listed, *which in this instance* coincides with the username. This could probably also be resolved by adding the username after `..../dav/files/` in the webdav hostname. – Saaru Lindestøkke Apr 11 '23 at 16:08
  • @SaaruLindestøkke I just tried it and it works as you said. Post this as an answer, and I'll accept it. – J'e Apr 11 '23 at 17:08
-1

Another alternative is to try os.scandir(), which is sometimes a sufficient method for listing files on the Nextcloud server, and is available in Python.

Sara
  • 1
  • 1