用户输入到文件路径

时间:2017-01-26 15:40:26

标签: python input sftp filepath

很抱歉这样的初学者问题,但我正在尝试创建一个脚本,用户可以使用该脚本从我的sftp服务器获取文件名。这些目录基本上是/ files / year / month / day,所以一个例子就是files / 2017/1/23。

我正在尝试使用用户输入来打印出他们想要的特定目录的文件名。我认为我的问题与将'/'合并到输入路径中有关。但这是我的第一个项目,我可以完全离开。感谢您的任何输入。

import pysftp as sftp

year = int(input("Enter year of event:  "))
month = int(input("Enter month of event:  "))
day = int(input ("Enter day of event:  "))

srv = sftp.Connection(host="hostname", username="user",
password="pass")

srv.chdir("recordings/'year'/'month'/'day'")

for filename in sorted(srv.listdir()):
    if filename.startswith('web') and filename.endswith('.mp4'):
        print (filename)

srv.close()

1 个答案:

答案 0 :(得分:0)

srv.chdir("recordings/'year'/'month'/'day'")行更改为

srv.chdir("recordings/%s/%s/%s"%(year,month,day))

希望这有帮助!

相关问题