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

M

Merrigan

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.

Thanks in advance for the help :)
 
D

Daniel Nogradi

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 gets rid of the first and last / and split splits the remaining
part and puts the results into a list.

HTH :)
 
M

Merrigan Took

Daniel said:
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 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!
 
M

Mike Kent

Python 2.4.2 (#1, Nov 29 2005, 14:04:55)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 
F

Fredrik Lundh

Merrigan said:
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.

os.path.split(path) splits a path into directory name and "base name"
parts.
('/usr/bin', 'python')

if you have a trailing slash, split will return an empty string for the base-
name part
('/home/testuser/projects', '')

but you can work around that by doing an extra split:
... path, name = os.path.split(path)
... 'projects'

hope this helps!

</F>
 
M

Michael Ekstrand

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.

Look at the os.path module, especially the basename and dirname
functions. basename will extract the "base name", that is, the last
component of a path.

- Michael
 
M

Merrigan

WOW, Thanks guys. I am really awestruck by the speed you guys relied
with. Thanks a lot for the help. I eventually found a way that did the
job for me.

I will definitely visit this Group more often.

Thanks for all the help! :D
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top