473,386 Members | 1,736 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,386 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 3625
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Glenn A. Harlan | last post by:
Why am I receiving the below error when calling - Path.GetTempFileName() The directory name is invalid. Description: An unhandled exception occurred during the execution of the current web...
39
by: Joe Laughlin | last post by:
If I have a character array with "/some/file/directory/file_name", are there any functions / libraries that I could use to separate the directory ("some/file/directory") from the file name...
3
by: James Coleman | last post by:
Hello, The following error is appearing when attempting to create a directory using the availale system.io methods: System.IO.DirectoryNotFoundException: Could not find a part of the path...
0
by: Jeremy | last post by:
I have an ASP.net application which needs to retreive images from multiple remote machines. It is reterieveing images generated by a web cam service on those machines. All of these machines,...
0
by: ruju00 | last post by:
I am getting an error in Login() method of the following class FtpConnection public class FtpConnection { public class FtpException : Exception { public FtpException(string message) :...
3
by: Chris Mellon | last post by:
This appears to be a change in behavior from Python 2.4 to Python 2.5, which I can't find documented anywhere. It may be windows only, or related to Windows behavior. In 2.4, the current...
4
by: tshad | last post by:
I have a site www.stf.com and a site www.stfstage.com (where I do all my testing). The problem is that www.stfstage.com is only internal and I need to get access from the outside (without...
3
by: Steph | last post by:
hello, i ve a probleme when deleting a directory and when i want create file immediatly after. 1) Directory.Delete(myPath, true); 2) TextWriter sw = new StreamWriter(myPath +"test.aspx"); i...
13
by: lawpoop | last post by:
Hello all - I have a two part question. First of all, I have a website under /home/user/www/. The index.php and all the other website pages are under /home/user/www/. For functions that are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.