Click here to Skip to main content
15,123,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The problem is as follows:

I built my python code in windows pc to analysis hundred thousands of files. Later, I found that the data are all located at Linux server (remote server). Now, I need to read the files from specific directory at Linux server. I could read one file 'data.nc'
But I have many files inside this directory
Could you please give me hints to iterate all files inside the given directory

What I have tried:

Python
import paramiko
import os
import xarray as xc

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='xxxxxxx', username='xxxxxx', password='xxxxxxx', port='22')
print('Connect to server')
sftp_client = ssh.open_sftp()

sftp_client.chdir('/home/abow/')


remote_file = sftp_client.open('data.nc')

try:
    ds = xc.open_dataset(remote_file)
    print(ds)

finally:
    remote_file.close()
# ssh.close()
# print('the connection is closed')
Posted
Updated 7-Jan-21 10:58am
v3
Comments
Richard MacCutchan 7-Jan-21 9:36am
   
"I got the error No such file or directory"
What exactly do you think anyone here can do about that? You need to check:
1. Does that directory actually exist on the server?
2. Do you have access to it via sftp?
EngAb1989 7-Jan-21 10:28am
   
Yes the file is available. However, I will reformulate the question now because I could read the file, but now I need to iterate inside the directory to read all files.

1 solution

After I spent long time. I reach to this solution and hope the others who face this problem can take this solution
Python
import paramiko
import os

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='xxxxxxxxxxx', username='xxxxxxxxx', password='xxxxxxxxx', port='22')
sftp_client = ssh.open_sftp()
sftp_client.chdir('/home/abow/nc_files')

# to iterate all files inside the current directory
for file in sftp_client.listdir(sftp_client.getcwd()):
    print(file)

sftp_client.close()

ssh.close()
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900