get_file_contents¶
-
FunctionalityWrapper.
get_file_contents
(rel_file_paths=None, style='rel_path', anon=False, read_mode='r')[source] Yields dicts containing the path and content of files on the FTP server.
Parameters: - rel_file_paths (str, list of str, None, default None) –
- None:
- The content of all files on the server will be retrieved.
- str or list of str:
- Only the content of those files will be retrieved.
- style ({'rel_path', 'url'}, default 'rel_path') –
- ‘rel_path’:
- Path relative to server_home/anon_root is returned.
- ’url’:
- A url to the file is returned.
- anon (bool) –
- True:
- return the filepaths/url of files in anon_root
- False:
- return the filepaths/url of files in in server_home
- read_mode ({'r', 'rb'}, default 'r') – Mode in which files should be read (see
open("filepath", read_mode)
)
Yields: content_dict (dict) – Dict containing the file path as relpath or url (see style) and the content of the file as string or bytes (see read_mode)
Raises: TypeError
– If rel_file_paths is notNone
, astr
or aniterable
TypeError
– If style is not astr
TypeError
– If anon is not abool
TypeError
– If read_mode is not astr
ValueError
– If the value of rel_file_paths or its items are not valid filepathsValueError
– If the value of style is not ‘rel_path’ or ‘url’ValueError
– If the value of read_mode is not ‘r’ or ‘rb’
Examples
Assuming a file structure as follows.
filesystem +---server_home +---test_file1.txt +---test_folder +---test_file2.zip
>>> list(ftpserver.get_file_contents()) [{"path": "test_file1.txt", "content": "test text"}, {"path": "test_folder/test_file2.txt", "content": "test text2"}]
>>> list(ftpserver.get_file_contents("test_file1.txt")) [{"path": "test_file1.txt", "content": "test text"}]
>>> list(ftpserver.get_file_contents("test_file1.txt", style="url")) [{"path": "ftp://fakeusername:qweqwe@localhost:8888/test_file1.txt", "content": "test text"}]
>>> list(ftpserver.get_file_contents(["test_file1.txt", "test_folder/test_file2.zip"], ... read_mode="rb")) [{"path": "test_file1.txt", "content": b"test text"}, {"path": "test_folder/test_file2.zip", "content": b'PK\x03\x04\x14\x00\x00...'}]
See also
- rel_file_paths (str, list of str, None, default None) –