- 1 year ago
Python pysftp get_r from Linux works fine on Linux but not on Windows
The issue you're facing with pysftp.get_r
on Windows might be related to the path format used in the function call. On Linux, paths are typically represented using forward slashes /
, while on Windows, paths use backslashes \
.
When using pysftp.get_r
to download files from a Linux server to a Windows machine, you need to ensure that the paths are in the correct format for Windows. One way to achieve this is by using Python's os.path.join
function to construct the local destination path.
Here's an example of how to use os.path.join
to ensure that the path format is correct for Windows:
pythonimport os
import pysftp
# Connect to the SFTP server
with pysftp.Connection('your_linux_server', username='your_username', password='your_password') as sftp:
# Set the remote directory
remote_dir = '/path/to/remote/directory/'
# Set the local directory (adjust this according to your Windows file system)
local_dir = 'C:/path/to/local/directory/'
# Get all files and directories recursively from the remote directory
sftp.get_r(remote_dir, local_dir)
In this example, we use os.path.join
to construct the local directory path with forward slashes. When the pysftp.get_r
function is called, it will use the local directory path in the correct format for Windows, which should resolve any issues with the function not working on Windows.
Make sure to adjust the remote_dir
and local_dir
variables to point to the correct directories on your Linux server and Windows machine, respectively.
Additionally, ensure that you have the necessary permissions and network connectivity to access the Linux server from your Windows machine using SFTP.