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

Merrigan Took merrigan at merrigan.za.net
Mon May 1 15:53:10 EDT 2006


Daniel Nogradi 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 :)
AAAAh, ok, Thank you, I will definitely look into this and play with it!



More information about the Python-list mailing list