I am trying to make a SFTP script to go through a directory and get all the files in it, inside this directory there are subdirectories that I would like to exclude. However when I use the stat.S_ISREG
function it gives an error of
OverflowError: mode out of range
From what I have read it seems like an error that Paramiko has with the handling of the bits that the server returns. Is there a way to solve this error, or some other way to differentiate files from folders based on the st_mode
? I was thinking of validating if the st_mode
is greater than 34000
exclude it and take it as a folder but I don't know if it is correct.
The code is the following:
client_ssh = paramiko.SSHClient()
client_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client_ssh.connect(host,username=user,password=pwd)
sftp_client = client_ssh.open_sftp()
df_file_info_sftp = pd.DataFrame([attr.__dict__ for attr in sftp_client.listdir_attr(directorio_remoto)]).sort_values("st_mtime", ascending=False)
for row in df_file_info_sftp.itertuples():
print (row.filename)
if stat.S_ISDIR(row.st_mode):
print('Carpeta:')
if stat.S_ISREG(row.st_mode):
print('Archivo:' + row.filename)
sftp_client.close
client_ssh.close
The error is:
From what I have read, it seems to be an error that Paramiko has with the handling of the bits returned by the server (apparently there are more than 16 and that is why the error)
There's no error in Paramiko. It's just that Paramiko (or actually your server) can return more permissons bits than Python stat
module can handle.
I've explained this in:
Pysftp "cd" fails against AIX server with "OverflowError: mode out of range" error
If you work with such server, you either have to analyze the bits without using the stat
functions, like:
S_IFMT = 0o170000
(row.st_mode & S_IFMT) == stat.S_IFDIR
Or trim the excess bites, before passing the value to stat
functions:
stat.S_ISDIR(row.st_mode & 0xFFFF)
Obligatory warning: Do not use AutoAddPolicy
this way – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".