如何在Python中写入文件时传递文件名

时间:2017-01-31 14:30:24

标签: python

我目前正在使用python脚本从SFTP服务器复制文件并将它们移动到我的本地计算机。此外,我正在尝试向我正在复制这些文件的服务器发送响应。我想知道如何在通知文本中传递文件名。

这是当前的代码:

location_FileImporter = "C:\\*\\*\\*\\*"
file_cache = location_FileImporter + "\\cache"
file_in = location_FileImporter + "\\in\\"
file_out = location_FileImporter + "\\out"
file_error = location_FileImporter + "\\error\\" + party + "_error.txt"
file_notification = location_FileImporter + "\\" + party + "_notification.txt"

#Puts notification file on the FTP server, notifying the 3rd party the file has been picked up.
print("\n(5/5) Notifying " + party + " how many files have been copied from the " + server + ".")
checkConnection()
try:
    notificationFile = open(file_notification, 'w') #Trying to create a new file or open one.
    notificationFile.write(str(nfiles_used) + " out of " + str(nfiles_copied) + " used.") #Append the error message to the file.
    if (nfiles_used != nfiles_copied):
        notificationFile.write("Only files with the \'.csv\' extension have been used.")
    notificationFile.close() #Close the file.
except:
    createErrorMessage(error_file_create_notification)
try:
    ftp.storlines("STOR " + str(time.asctime(time.localtime(time.time()))) + " - notification.txt", open(file_notification, 'rb'))
    print("--> Successfully created the \'notification.txt\' file on the " + party + " " + server + ".")
    os.remove(file_notification)
except:
    createErrorMessage(error_file_set_notification)
print("--------------------------------------------------------")

在上面的代码中,我试图将文件从SFTP复制到缓存,然后将它们复制到输入文件夹。

0 个答案:

没有答案