How do I take a directory name from a given dir?

Steve Poe steve.poe at gmail.com
Mon May 1 22:06:13 CEST 2006


Daniel,

Before writing your script, what is your general approach to backup
configuration files? Do you tar/zip them into one file? Do you ftp/ssh each
file to another server?

One approach I've used in backing up files from another server is:
from ftplib import FTP

def handleDownload(block):
    file.write(block)
#    print "."
os.chdir('/home/backup')
ftp = FTP('10.0.0.2')
ftp.login('backuserid','backuppasswd')
ftp.cwd('/home/backup')
# Start downloading
filename = 'main_config.tar.bz2'
file = open(filename,'wb')
print 'Getting ' + filename
ftp.retrbinary('RETR ' + filename, handleDownload, 1024)
print 'Closing file ' + filename
file.close()
#Finish downloading

The 'Start downloading' and 'Stop downloading' are what I repeat for each
file I need. I am sure there are better ways, but this works. I imagine you
could do this with wildcards as well.

Good luck.

Steve Poe


On 5/1/06, Daniel Nogradi <nogradi at gmail.com> wrote:
>
> > Hi,
> >
> > I am trying to write a script to backup all my company's server configs
> > weekly. The script will run in a cronjob, on whatever I set it.
> >
> > The issue I am currently having isto "extract" the directory name from
> > a given directory string. For example: from the string
> > "/home/testuser/projects/" I need to extract the "projects" part. The
> > problem is that the directory names that needs to be used will never be
> > the same lenght, so as far as my (very limited) knowledge stretches,
> > slicing and indicing is out of the question.
> >
> > Can anybody help me, or give me an idea of what I should look at,
> > seeing as I'm seriously stuck. I only started coding at the beginnig of
> > the year, and was never interested before that, so my knowlege is
> > basically non-existent.
>
> You can try this:
>
> >>> '/home/testuser/projects/'.strip( '/' ).split( '/' )
> ['home', 'testuser', 'projects']
>
> strip gets rid of the first and last / and split splits the remaining
> part and puts the results into a list.
>
> HTH :)
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060501/cff7d750/attachment.html>


More information about the Python-list mailing list