views:

184

answers:

3

Running the following code:

import os
import datetime
import ftplib

currdate = datetime.datetime.now()
formatdate = currdate.strftime("%m-%d-%Y %H%M")

def log():

    fqn = os.uname()[1]
    ext_ip = urllib2.urlopen('http://whatismyip.org').read()
    log = open ('/Users/admin/Documents/locatelog.txt','w')
    log.write(str("Asset: %s " % fqn))
    log.write(str("Checking in from IP#: %s" % ext_ip))
    smush = str(fqn +' @ ' + formatdate)
    os.rename('/Users/admin/Documents/locatelog.txt','/Users/admin/Documents/%s.txt' %  smush )

    s = ftplib.FTP('10.7.1.71','username','password')
    f = open('/Users/admin/Documents/%s.txt' % smush,'r')
    s.storbinary("STOR /Users/admin/Documents/%s.txt" % smush,f)

Generates the following error:

ftplib.error_perm: 550 /Users/admin/Documents/678538.local @ 02-24-2010 1301.txt: No such file or directory

I have a feeling something is amiss in this line :

s.storbinary("STOR /Users/admin/Documents/%s.txt" % smush,f)

678538 is the host I am testing on...using Mac OS X 10.5 and Python 2.5.1

+5  A: 

Shouldn't it bef = open('/Users/admin/Documents/%s.txt' % smush,'r') ? notice the / in front of Users

If you dont put the first /, the script will think the path to the file is relative to the current directory (where the script is run from)

Edit:

I m not too familiar with Python (I wish) but shouldnt it be:

s.storbinary('STOR /Users/admin/Documents/%s.txt' % smush,f) ?

In your example, Python will treat your string as literal and you want to interpolate the value of smush with %s

Edit 2:

Does the directory /Users/admin/Documents/ exist on your server? If not, I think you will have to create them before copying anything. (Since the error message is about some files/folders missing).

You can create them yourself first. Run your script. If the file is copied successfully, then you can add the creation of the directories from within your script.

ccheneson
the 'full' source shows the /, but the one liner does not. It would appear the snippet is what is running, but the full source is what is desired.
KevinDTimm
@ ccheneson thanks..I did forget that slash, but the other error I have is with the ftplib command ..seems to be doing the same thing?( i edited the question to reflect changes )
CaseyIT
@skylarking: See my edit
ccheneson
@ccheneson -- Thanks you've got a good eye. That formatting worked, but it's still throwing an error ( updated above ) I think I am messing something up with ftplib...
CaseyIT
+1  A: 

remove all spaces from file name .eg in smush = str(fqn +' @ ' + formatdate), you are putting a space in front of and after "@". you path looks like

/Users/admin/Documents/something @ something

and when you pass it to ftplib, it may have problem. another way is to try putting quotes, eg

s.storbinary("STOR '/Users/admin/Documents/%s.txt'" % smush,f)
ghostdog74
A: 

Edit:

This version works: Problem was that I was writing to buffer, and not to file.

import os
import urllib2
import datetime
import ftplib

currdate = datetime.datetime.now()
formatdate = currdate.strftime("%m-%d-%Y-%H%M")

def log():

    fqn = os.uname()[1]
    ext_ip = urllib2.urlopen('http://whatismyip.org').read()
    smush = str(fqn + formatdate)
    s = ftplib.FTP('10.7.1.71','username','password')
    f = open('/Users/admin/Documents/%s.txt' % smush,'w')
    f.write(str("Asset: %s " % fqn))
    f.write('\n')
    f.write(str("Checking in from IP#: %s" % ext_ip))
    f.write('\n')
    f.write(str("On: %s" % formatdate))
    f.close
    f = open('/Users/admin/Documents/%s.txt' % smush,'rb')
    s.storbinary('STOR %s.txt' % smush , f)
    s.close
    f.close
CaseyIT