Skip to content
Advertisement

Read a TSV file from a remote server

I have this function which returns the path to the file I need to read

def specie_to_file(specie):
    switcher = {
    'human': 'sftp://eliran@SERVER/PATH/TO/FILE1',
    'mouse': 'sftp://eliran@SERVER/PATH/TO/FILE2'
    }
    return switcher.get(specie, None)

Later on, I am trying to open the file

database = pd.read_csv(db_file, sep='t')

db_file holds one of the paths above.

When I execute the script I get this error:

Traceback (most recent call last):
.
.
.
  File "gene_converter.py", line 111, in converter
    database = pd.read_csv(open(db_file), sep='t')
FileNotFoundError: [Errno 2] No such file or directory: 'sftp://eliran@SERVER/PATH/TO/FILE1'

I have checked the files names and the paths they all exist and in the right location.

I have tried the following and got the same traceback:

database = pd.read_csv(Path(db_file), sep='t')
database = pd.read_csv(open(db_file,'r'), sep='t')
database = pd.read_csv(open(db_file,'r').read(), sep='t')

Advertisement

Answer

Your file is on FTP server.

Use paramiko in order to read it.

with sftp.open("sftp://eliran@SERVER/PATH/TO/FILE1") as f:
    pd.read_csv(f)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement