Python FTP troubles

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Python FTP troubles

Post by insomaniacal on Fri Dec 04, 2009 8:21 pm
([msg=31011]see Python FTP troubles[/msg])

Hi everyone. I'm having a little bit of trouble getting a piece of code to work correctly here.

Here is the code.

Code: Select all
filename = "./" + `random.randrange(0,9999999999999999999999999999999999)` + ".txt"
    f = open(filename, 'w')
    f.write(`data`)
    f.close

   
    f = open(filename,'rb')
    ftp = FTP('myftphost.com')
    time.sleep(5)
    ftp.login('user,pass')
    time.sleep(5)
    ftp.storbinary('STOR ``filename``', f )
   


Everything works as it is supposed to. The variable "filename" is saved in the current directory with the randomly generated name. When the ftp.storbinary command runs, it uploads the information in the file correctly, however, it always saves it with the name, `filename`. This causes it to overwrite itself each time that it uploads.

Basically, how can I get this to upload the file with the file's actual randomly generated name instead of "`filename`". Any advice would be very helpful, using storbinrary would be nice, but another command or even entire module is ok, as long as it solves my problem.

I've also posted this on Daniweb, so sorry if it looks familiar ;p
It's not who votes that counts, it's who counts the votes
insomaniacal.blog.com
User avatar
insomaniacal
Addict
Addict
 
Posts: 1210
Joined: Sun May 24, 2009 10:21 am
Blog: View Blog (0)


Re: Python FTP troubles

Post by Goatboy on Fri Dec 04, 2009 10:46 pm
([msg=31017]see Re: Python FTP troubles[/msg])

I'm not a python expert, but might it have something to do with the double backticks in the last line?

Code: Select all
ftp.storbinary('STOR ``filename``', f )

If I recall correctly, single backticks means to replace the variable name with the content. So would double backticks negate that? I would think it should be:

Code: Select all
ftp.storbinary('STOR `filename`', f )

Just a guess though; someone else may have more useful input.
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2820
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: Python FTP troubles

Post by insomaniacal on Fri Dec 04, 2009 11:34 pm
([msg=31018]see Re: Python FTP troubles[/msg])

That's what I originally did, and I added in the extra backticks in hopes that it would fix something. However, it would just upload with the filename having either one or two backticks.

I fixed the problem by just adding another command that is to be sent to the FTP server.

Code: Select all
ftp.rename("``filename``",filename)


Though someone else suggested that I store the command in one variable, and then just pass that variable to the ftp command, like this.

Code: Select all
A = "STOR" + `filename`
ftp.storbinary(A,F)

Haven't tried this way though, since the renaming since to work fine.

*Sorry if my syntax is off, it's late, and I'm just doing this to give the jist of the method.
It's not who votes that counts, it's who counts the votes
insomaniacal.blog.com
User avatar
insomaniacal
Addict
Addict
 
Posts: 1210
Joined: Sun May 24, 2009 10:21 am
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests