1

I need to get files from server and next write them to database. I use:

with pysftp.Connection(host, username=username, password=password, cnopts=cnopts) as sftp:
    sftp.chdir('path')
    all_files = [file_name for file_name in sftp.listdir() if 'access' in file_name]
    today_log = all_files[0]
    all_files = all_files[1:]
    df = df.append(sftp.get(str(today_log)))

But it returns None. How can I get content from file and write it to dataframe?

martineau
  • 119,623
  • 25
  • 170
  • 301
Petr Petrov
  • 4,090
  • 10
  • 31
  • 68
  • What returns `None`? – martineau Jun 01 '17 at 14:18
  • @martineau content of file is empty and it returns `None`, but it doesn't empty. I need to read that and add to dataframe – Petr Petrov Jun 01 '17 at 14:19
  • What is the value of `all_files`? – errata Jun 01 '17 at 14:20
  • @errata It's list. I filter list in directory and next I add it's name to `sftp.get()` – Petr Petrov Jun 01 '17 at 14:21
  • @PetrPetrov I know it is a list, I was asking for a value of it? :) – errata Jun 01 '17 at 14:24
  • @errata: `all_files` is created in the third line of the code. – martineau Jun 01 '17 at 14:25
  • @martineau I can see that :) – errata Jun 01 '17 at 14:27
  • @errata: Didn't sound like it. Anyhow, seems like it would be more pertinent to ask what the value of `today_log` was when the `get()` call returned `None`. – martineau Jun 01 '17 at 14:29
  • @martineau My initial question was "what is the value of `all_files`", not what type of variable is it or in which line is it :) Since `today_log` seems to be `None`, I was wondering what is the value of `all_files` because `today_log` should be the first element of `all_files`... :) – errata Jun 01 '17 at 14:32
  • @errata there are names of files, that I need to get from directory. I try to get content of file with it's name. But it can't be None, because it's size is 45Mb – Petr Petrov Jun 01 '17 at 14:39
  • @PetrPetrov Do you see the desired output (content) of that file when you run `sftp.open(all_files[0])` (immediately after the first `all_files = ...` line)? Btw, if I am right, `sftp.listdir()` will return you files in "arbitrary" order, meaning that you might not get the same file in position `0` every time... – errata Jun 01 '17 at 14:48
  • @errata `sftp.open()` returns ``. How can I show data from it? – Petr Petrov Jun 01 '17 at 14:51
  • @errata can you say, i check my directory with code, and now there are some files from server. What should I use only to read that, but not saving to my pc? – Petr Petrov Jun 01 '17 at 15:01

1 Answers1

1

If I understand your question correctly, you want to read the file from SFTP server into a variable/memory.

For that, you need to use .getfo, like:

flo = BytesIO()
sftp.getfo(remotepath, flo)

Alternatively, use Paramiko library directly (without the pysftp wrapper).
See Read a file from server with ssh using python.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992