473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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 :)

May 1 '06 #1
6 1330
> 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 :)
May 1 '06 #2
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!
May 1 '06 #3
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.
import os
d = "/home/testuser/projects"
os.path.basename(d) 'projects' os.path.dirname(d) '/home/testuser'


May 1 '06 #4
"Merrigan" wrote:
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.
import os
os.path.split("/usr/bin/python") ('/usr/bin', 'python')

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

but you can work around that by doing an extra split:
path = "/home/testuser/projects/"
path, name = os.path.split(path)
if not name: ... path, name = os.path.split(path)
... path '/home/testuser' name

'projects'

hope this helps!

</F>

May 1 '06 #5
On Mon, May 01, 2006 at 12:42:58PM -0700, Merrigan wrote:
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

--
mouse, n: a device for pointing at the xterm in which you want to type.
-- Fortune
Visit me on the Web: http://www.elehack.net
May 1 '06 #6
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

May 1 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: huzz | last post by:
I have web application that quaries the Active Directory to get user details.. everything works fine but someday I'll get System.Runtime.InteropServices.COMExection and if I restart the client...
2
by: 73blazer | last post by:
Hello, I'm writing some C++ code, and I need to be able to find the number of files in a given directory. Is it possible under AIX4.3.3 with C++ 3.6.4? I cannot seem to locate anything of this...
6
by: Christopher | last post by:
What is the syntax for including files from another directory? In my case I want to include a file called "error.h" which lies in a directory called "support" which is a directory inside the...
8
by: James Owens | last post by:
I'm a relative newbie, interested in storing the information from several server directories and subdirectories in XML so that I can present it selectively using XSL (all files updated today or...
10
by: Pieter Coucke | last post by:
Hi, I need to find all the files in a directory which are younger than a given date (modification date or creation date). Also I need to find a way to find all files in a directory which are...
10
by: mansimisha | last post by:
Hi to everybody.. I need a help need to have a program to represent directory structure(a directory can contain any number of directory or files) alos write a programme to insert one element nd...
2
by: graphicsxp | last post by:
Hi, How can I open all the files in a directory, which names match a particular string ? Say I have a string like 'a file name to find' and I want to find and open all the files of a given...
3
by: siyaverma | last post by:
i am trying to upload csv file from user's computer to main server the code i am using is if(((isset($_GET)) && ($_GET=="yes")) ) { $typefield = $_GET; echo...
0
by: rbukkara | last post by:
Hi, I have got the following error while trying to add a user in the LDAP Directory. javax.naming.NameNotFoundException: ; remaining name 'uid=vassila,ou=People,dc=cs,dc=uno,dc=edu' I have...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.