471,888 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Getting the Directory/Path details

Hi,
I could not understand how I can do the following things in Python.
Please help me.

1. First I have to find the current directory from where the script is
invoked.
2. Next I have to form a directory structure there. If the current
directory in step 1 is /home/mylogin, then from there I have to build
a directory structure like
/home/mylogin/result
/home/mylogin/tmp/
/home/mylogin/.....

There are three things as I look at it. First determing the current
directory path. Second adding a string to it like /result etc. Third
creating the new directory.
Thank you for the help.

Kali
Jul 18 '05 #1
3 3513
Kali K E wrote:
Hi,
I could not understand how I can do the following things in Python.
Please help me.

1. First I have to find the current directory from where the script is
invoked.
2. Next I have to form a directory structure there. If the current
directory in step 1 is /home/mylogin, then from there I have to build
a directory structure like
/home/mylogin/result
/home/mylogin/tmp/
/home/mylogin/.....

There are three things as I look at it. First determing the current
directory path. Second adding a string to it like /result etc. Third
creating the new directory.
Thank you for the help.

Kali


1 os.getcwd()
2 os.path.join()
3 os.mkdir() or os.makedirs()

<code>
import os

subfolders = [
"result",
"temp",
"something/else",
]

folder = os.getcwd()

for sf in subfolders:
os.makedirs(os.path.join(folder, sf))
</code>
Jul 18 '05 #2
Peter Otten <__*******@web.de> wrote in message news:<bk*************@news.t-online.com>...
Kali K E wrote:
Hi,
I could not understand how I can do the following things in Python.
Please help me.

1. First I have to find the current directory from where the script is
invoked.
2. Next I have to form a directory structure there. If the current
directory in step 1 is /home/mylogin, then from there I have to build
a directory structure like
/home/mylogin/result
/home/mylogin/tmp/
/home/mylogin/.....

There are three things as I look at it. First determing the current
directory path. Second adding a string to it like /result etc. Third
creating the new directory.
Thank you for the help.

Kali


1 os.getcwd()
2 os.path.join()
3 os.mkdir() or os.makedirs()

<code>
import os

subfolders = [
"result",
"temp",
"something/else",
]

folder = os.getcwd()

for sf in subfolders:
os.makedirs(os.path.join(folder, sf))
</code>


Hi,

Thank you very much for the answer. It works fine when used first
time. But when the directory already exists I am getting the following
traceback message.
Traceback (most recent call last):
File "temp6.py", line 15, in ?
os.makedirs(os.path.join(folder, sf))
File "/usr/lib/python2.2/os.py", line 203, in makedirs
mkdir(name, mode)
OSError: [Errno 17] File exists:
'/hda1/home/kalike/chnages/python/result'

How do I take care in such a case?

Also can you please let me know where I can search for all these OS or
system calls. Is it available as a document some where?

Thanks,
Kali
Jul 18 '05 #3
Kali K E wrote:
Thank you very much for the answer. It works fine when used first
time. But when the directory already exists I am getting the following
traceback message.
Traceback (most recent call last):
File "temp6.py", line 15, in ?
os.makedirs(os.path.join(folder, sf))
File "/usr/lib/python2.2/os.py", line 203, in makedirs
mkdir(name, mode)
OSError: [Errno 17] File exists:
'/hda1/home/kalike/chnages/python/result'

How do I take care in such a case?
I modified the code to accept already existing directories. The strategy is:
Try to create the directory
If creation fails, see if the directory already exists.
If yes move to the next directory, otherwise throw an exception.

class DirectoryException(Exception): pass

for sf in subfolders:
dirpath = os.path.join(folder, sf)
try:
os.makedirs(dirpath)
except OSError, e:
if e.errno == 17:
if not os.path.isdir(dirpath):
raise DirectoryException, "'%s' can neither be created nor
is it an existing directory" % dirpath
else:
raise # cannot handle it here

Also can you please let me know where I can search for all these OS or
system calls. Is it available as a document some where?


It seems you are not familiar with the concept of Exceptions. So, if you
were already exposed to another programming language, I'd suggest reading
the tutorial that comes with your distribution first, otherwise look out
for a textbook.

The relevant links on the python site are (in recommended reading order):

http://www.python.org/doc/current/tut/tut.html
http://www.python.org/doc/current/li...e-os.path.html
http://www.python.org/doc/current/lib/module-os.html

Peter

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

39 posts views Thread by Joe Laughlin | last post: by
3 posts views Thread by James Coleman | last post: by
reply views Thread by Jeremy | last post: by
3 posts views Thread by Chris Mellon | last post: by
4 posts views Thread by tshad | last post: by
3 posts views Thread by Steph | last post: by
13 posts views Thread by lawpoop | last post: by
reply views Thread by zermasroor | last post: by

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.