2017-01-26 2743 views
1

對不起,這樣一個初學者的問題,但我想創建一個腳本,用戶可以用來從我的sftp服務器獲取文件名。目錄基本上是/文件/年/月/日,因此一個例子是文件/ 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() 

回答

0

變化srv.chdir("recordings/'year'/'month'/'day'")線以下

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

希望這有助於!

+0

謝謝Jay!這就像一個冠軍,給了我更多的代碼來了解這個字符串格式。 –

+0

歡迎您。如果有效,你可以提高我的答案:P –

+0

我有,但我沒有足夠的聲譽讓它公開顯示。 –