0

I'm trying to call subprocess from a python script. Script would call 'lftp' on linux with specific parameters as shown below. The problem as that I can not pass filename (filename will be different every day).

I was trying almost every combination but without success (for example: ${fname}, $fname, {fname} and so on). I'm running out of ideas so I'm asking for a help.

Every time I get response from ftps server Access failed: 550 The system cannot find the file specified. I can properly log on and change folder.

import subprocess
import datetime


fname=different_every_day

proc=subprocess.call(
    ["lftp", "-u", "user:password", "ftps://servername:990", "-e",
     "set ftp:ssl-protect-data true; set ftp:ssl-force true; "
     "set ssl:verify-certificate no;get ${fname}"])

print(proc)

P.S. Close to proper answer was wagnifico, so i will accept his answer but for others who need solution it suppose to be as below:

proc=subprocess.call(["lftp","-u","user:pass","ftps://example.something","-e","set ftp:ssl-protect-data true; set ftp:ssl-force true; set ssl:verify-certificate no;cd Ewidencja;pget "+'"'+fname+'"'])
admfotad
  • 197
  • 3
  • 11
  • could you send me the eentire comand that you need to run on command line please? – Jasar Orion Oct 29 '20 at 12:54
  • Actually this is almost entire command but i will edit code in question. All i'm trying to do is that filename on ftps server will be different every day - it is generated automatically, for example today's filename will be: 2020-10-29 - All computers.xls , tomorrow there will be 2020-10-30 - All computers.xls and so on – admfotad Oct 29 '20 at 13:06

2 Answers2

0

You are mixing python and environment variables.

When you use ${fname}, bash considers fname an environment variable, something known by your OS. Since it is not defined, it will use an empty value, thus, not finding the file.

You either need to define fname in your terminal and them call it in python, as in the question:

export fname='2020-10-29 - All computers.xls'
python your_code.py

Also, you need to add the flag shell=True when you call subprocess.call

Or define it entirely in python:

fname='2020-10-29 - All computers.xls'
proc=subprocess.call(
    ["lftp", "-u", "user:password", "ftps://servername:990", "-e",
     "set ftp:ssl-protect-data true; set ftp:ssl-force true; "
     "set ssl:verify-certificate no;get " + fname])
wagnifico
  • 632
  • 3
  • 13
  • 1
    The first command won't work unless you also `export fname`. A better solution is `fname='whatever' python your_code.py` in a single command, which sets `fname` just for the duration of the Python process. Or maybe refactor `your_code.py` to accept a filename as a command-line argument, which you can then pick up from `sys.argv[1]`. – tripleee Oct 29 '20 at 13:43
  • You are correct, I will edit my answer. – wagnifico Oct 29 '20 at 13:54
-1

here try it:

import os
import time
def python_to_bash(cli_args):
    output = os.popen(cli_args).read()    
    return output

file_name = str(time.time())+".xls"
python_to_bash("lftp -u user:password ftps://servername:990 -e set ftp:ssl-protect-data true set ftp:ssl-force true set ssl:verify-certificate no get "+file_name)

i dont know if the command that you need is right but when i need to make any dynamic names i use in this form

Jasar Orion
  • 626
  • 7
  • 26
  • I think that problem is after '-e' because there suppose to be '"' at the beginning and at the end , for example it should looks like: (...) -e "extra commands; get filename" – admfotad Oct 29 '20 at 13:41
  • use simple commas ' at the start of python to bash and the end – Jasar Orion Oct 29 '20 at 13:43
  • here: `python_to_bash('command here'+filename+' "other options '+dinamic_options+'"')` – Jasar Orion Oct 29 '20 at 13:43
  • Somebody closed my post but i allready have solution for others: proc=subprocess.call(["lftp","-u","user:pass","ftps://example.something","-e","set ftp:ssl-protect-data true; set ftp:ssl-force true; set ssl:verify-certificate no;cd some_folder;pget "+'"'+fname+'"']) – admfotad Oct 29 '20 at 14:06