python - 带有 lftp 变量的子进程
问题描述
我正在尝试从 python 脚本调用子进程。脚本将在 linux 上使用特定参数调用“lftp”,如下所示。我无法传递文件名的问题(文件名每天都会不同)。
我尝试了几乎所有组合,但都没有成功(例如:${fname}
、、$fname
等等{fname}
)。我的想法不多了,所以我正在寻求帮助。
每次我得到来自 ftps 服务器的响应Access failed: 550 The system cannot find the file specified
。我可以正确登录并更改文件夹。
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)
PS接近正确答案是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+'"'])
解决方案
您正在混合 python 和环境变量。
当您使用 时${fname}
,bash 会考虑fname
一个环境变量,这是您的操作系统已知的。由于未定义,它将使用空值,因此找不到文件。
您要么需要fname
在终端中定义,然后他们在 python 中调用它,如问题所示:
export fname='2020-10-29 - All computers.xls'
python your_code.py
此外,您需要在shell=True
调用 subprocess.call 时添加标志
或者完全在 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])
推荐阅读
- javascript - React Hook Form - 手动更新预注册对象中的一些字段
- c# - 无法在 Ubuntu 主机上运行已编译的 .Net Core 应用程序
- mysql - 我可以在触发器中进行查询吗?
- azure-service-fabric - 同一个 Service Fabric Actor 应用程序中是否有大量的 Actor?
- python - 如何打印 Altair 默认配置的字典
- sed - 如何使用 sed 替换 bash 中第 m 次到第 n 次出现的字符串?
- python - 计算 Dask Dataframe 中特定值的所有出现次数
- eclipse - Intellij 将现有资源导入包
- loops - 满足条件时标记为 1 n 个连续行
- c# - 网站未下载由站点 C#/ASP.NET MVC 创建的 PDF