Help implementing an idea

  • Thread starter Nicholas.Vaidyanathan
  • Start date
N

Nicholas.Vaidyanathan

Well, I'm a total python n00b, but I was playing around with exception handling
yesterday, and was stricken by how incredibly easy it is to use the op system
to create nice scripts... I did the following:

import sys
lines = sys.stdin.readlines()
lines.sort()
for stuff in lines:
print stuff ,

just to copy stuff from one file to another, and was quite impressed with the
results. Now, I was thinking today, I'd really like to create a program that
can go to a specific directory and upload all the files in the directory to a
specific url for backup purposes, and I have the feeling that the python
implementation would be ruthlessly small and efficient, like the
above....anyone who could point me in the right direction as to how to actually
do it?
 
R

rh0dium

Try this..


#!/usr/bin/env python
# Upload a file to a FTP server

from sys import argv, exit
from ftplib import FTP

if len(argv) != 6:
print 'Incorrect number of parameters'
print 'USAGE: upload.py <server> <username> <password> <local
file>
<remote file>'
exit(0)

server = argv[1]
username = argv[2]
password = argv[3]
upfile = argv[5]
downfile = argv[4]

try:
print 'Connecting to %s' % server
ftp = FTP(server)
ftp.login(username, password)
print 'Connection to %s opened' % server
#send file in binary mode to server (STOR command sets remote
filename)
ftp.storbinary('STOR %s' % upfile, open(downfile,'r'))
ftp.quit()
print 'File %s uploaded' % upfile
except Exception, err:
print 'Error uploading file. Error: %s' % err
 
T

Tim Roberts

Well, I'm a total python n00b, but I was playing around with exception handling
yesterday, and was stricken by how incredibly easy it is to use the op system
to create nice scripts... I did the following:

import sys
lines = sys.stdin.readlines()
lines.sort()
for stuff in lines:
print stuff ,

Or sys.stdout.writelines( lines ).
just to copy stuff from one file to another, and was quite impressed with the
results. Now, I was thinking today, I'd really like to create a program that
can go to a specific directory and upload all the files in the directory to a
specific url for backup purposes, and I have the feeling that the python
implementation would be ruthlessly small and efficient, like the
above....anyone who could point me in the right direction as to how to actually
do it?

How do you want to do the upload? FTP? That's easy. Check ftplib.py.
 
F

Fuzzyman

results. Now, I was thinking today, I'd really like to create a program that
can go to a specific directory and upload all the files in the directory to a
specific url for backup purposes, and I have the feeling that the python
implementation would be ruthlessly small and efficient, like the
above....anyone who could point me in the right direction as to how to actually
do it?

Upload to a URL *sounds* like you want to do the upload by http rather
than ftp.

In this case you can do it with a Python CGI on the server and a script
on the client that communicates with the CGI.

When voidspace is back up you can find a pair of scripts that do just
that :

http://www.voidspace.org.uk/python/cgi.shtml#upload

Hint - filelist = [os.path.join(directory, entry) for entry in
os.listdir(directory)]

Regards,

Fuzzy
http://www.voidspace.org.uk/python
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top