Search code examples
pythonftpftplib

Using variable in a name of uploaded file in Python ftplib


I'm pushing a file to my FTP, and I have

file = open(home_path + '\\Desktop\\' + checkuser + ' Report.txt', 'rb')
session.storbinary('STOR file.txt', file) 

This I believe copies the content from the file on my Desktop and creates a file file.txt on the FTP;

I can't seem to be able to create a file name on the FTP that is the same as the one on my Desktop (or any desktop/user), something like

session.storbinary('STOR' + home_path + '\\Desktop\\' + checkuser + ' Report.txt', file)

Is there a way around this?

*PS: running

sys.stdout = open(home_path + '\\Desktop\\' + checkuser + ' Report.txt', 'a')

works just fine, it creates a file named currentuser Report.txt.


Solution

  • I'm not sure I understand you problem. But maybe you want to do this?

    session.storbinary('STOR ' + checkuser + ' Report.txt', file)
    

    (assuming the checkuser value is a part of the filename)

    And note the space after STOR (which is missing in your code).