Python 具有lftp变量的子流程

Python 具有lftp变量的子流程,python,subprocess,lftp,Python,Subprocess,Lftp,我正在尝试从python脚本调用子流程。脚本将在linux上使用如下所示的特定参数调用“lftp”。问题是我无法传递文件名(文件名每天都会不同) 我尝试了几乎所有的组合,但都没有成功(例如:${fname},$fname,{fname}等等)。我想不出主意了,所以我想寻求帮助 每次从ftps服务器获得响应时,系统都无法找到指定的文件。我可以正确登录并更改文件夹 import subprocess import datetime fname=different_every_day proc=

我正在尝试从python脚本调用子流程。脚本将在linux上使用如下所示的特定参数调用“lftp”。问题是我无法传递文件名(文件名每天都会不同)

我尝试了几乎所有的组合,但都没有成功(例如:
${fname}
$fname
{fname}
等等)。我想不出主意了,所以我想寻求帮助

每次从ftps服务器获得响应时,系统都无法找到指定的文件。我可以正确登录并更改文件夹

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)
注:接近正确答案的是wagnifico,因此我将接受他的答案,但对于其他需要解决方案的人,假设答案如下:

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+'"'])
在这里尝试一下:

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)

我不知道您所需要的命令是否正确,但是当我需要在这个表单中使用任何动态名称时,您正在混合python和环境变量

当您使用
${fname}
时,bash认为
fname
是一个环境变量,是您的操作系统所知道的。由于未定义它,它将使用空值,因此无法找到该文件

您需要在终端中定义
fname
,然后他们用python调用它,如问题所示:

export fname='2020-10-29 - All computers.xls'
python your_code.py
此外,在调用subprocess.call时,还需要添加标志
shell=True

或者完全用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])

你能把你需要在命令行上运行的代码发给我吗?实际上这几乎是整个命令,但我会编辑有问题的代码。我所要做的就是ftps服务器上的文件名每天都会不同——它是自动生成的,例如今天的文件名是:2020-10-29-All computers.xls,明天将是2020-10-30-All computers.xls,所以我认为问题出在“-e”之后,因为假设有例如,“'在开头和结尾,它应该看起来像:(…)-e”额外命令;get filename“在python to bash的开头使用简单逗号”,结尾:
python to bash('command here'+filename+'“other options'+dinamic_options+”)
有人关闭了我的帖子,但我已经为其他人准备好了解决方案:proc=subprocess.call([“lftp”、“-u”、“用户: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+”””])第一个命令将不起作用,除非同时
导出fname
。更好的解决方案是在单个命令中使用
fname='where'python your_code.py
,它仅在python进程期间设置
fname
。或者重构
您的\u code.py
以接受文件名作为命令行参数,然后您可以从
sys.argv[1]
中选择该参数。如果您是正确的,我将编辑我的答案。