472,330 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

How to find out the directory that the py file is in?

I have the following python script and some_command in the same
directory. I have to call the python script from that directory.
Otherwise, some_command won't be found. I'm wondering how to make the
following script working no matter what the working directory is.

Thanks,
Peng

#!/usr/bin/env python

import os

cmd = "./some_command";
print cmd
os.system(cmd);

Oct 24 '06 #1
5 1684
At Tuesday 24/10/2006 20:39, Pe*******@gmail.com wrote:
>I have the following python script and some_command in the same
directory. I have to call the python script from that directory.
Otherwise, some_command won't be found. I'm wondering how to make the
following script working no matter what the working directory is.
print os.path.abspath(__file__)
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ”gratis!
”Abrķ tu cuenta ya! - http://correo.yahoo.com.ar
Oct 24 '06 #2


On Oct 24, 7:01 pm, Gabriel Genellina <gagsl...@yahoo.com.arwrote:
At Tuesday 24/10/2006 20:39, PengYu...@gmail.com wrote:
I have the following python script and some_command in the same
directory. I have to call the python script from that directory.
Otherwise, some_command won't be found. I'm wondering how to make the
following script working no matter what the working directory is.print
os.path.abspath(__file__)
How to get the absolute dir name which the file is in? Is there any
function can be called easily?

Thanks,
Peng

Oct 25 '06 #3
import os
print os.path.dirname(os.path.abspath(__file__))

--
Jerry

Oct 25 '06 #4
Pe*******@gmail.com wrote:
>
On Oct 24, 7:01 pm, Gabriel Genellina <gagsl...@yahoo.com.arwrote:
>>At Tuesday 24/10/2006 20:39, PengYu...@gmail.com wrote:

>>>I have the following python script and some_command in the same
directory. I have to call the python script from that directory.
Otherwise, some_command won't be found. I'm wondering how to make the
following script working no matter what the working directory is.print

>>os.path.abspath(__file__)


How to get the absolute dir name which the file is in? Is there any
function can be called easily?

Thanks,
Peng
That would be

os.path.split(os.path.abspath(__file__))[0]

You *are* planning to read the documentation at some stage, right? ;-)

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Oct 25 '06 #5
On Wed, 2006-10-25 at 02:49 +0100, Steve Holden wrote:
Pe*******@gmail.com wrote:

On Oct 24, 7:01 pm, Gabriel Genellina <gagsl...@yahoo.com.arwrote:
>At Tuesday 24/10/2006 20:39, PengYu...@gmail.com wrote:
I have the following python script and some_command in the same
directory. I have to call the python script from that directory.
Otherwise, some_command won't be found. I'm wondering how to make the
following script working no matter what the working directory is.print
>os.path.abspath(__file__)

How to get the absolute dir name which the file is in? Is there any
function can be called easily?

Thanks,
Peng

That would be

os.path.split(os.path.abspath(__file__))[0]

You *are* planning to read the documentation at some stage, right? ;-)
More simple:

os.path.dirname(os.path.abspath(__file__))

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Oct 25 '06 #6

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

Similar topics

3
by: Greg Yasko | last post by:
Hi. Does anyone know if there's an equivalent of Perl's file::find module in Python? It traverses a directory. I've googled extensively and checked...
1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import...
1
by: Thieum22 | last post by:
Hi, I try to go through a directory and it's subdirectories to reah the properties of each files. But I have a problem to set active the...
1
by: Dan Jones | last post by:
I'm writing a script to process a directory tree of images.Ā Ā InĀ each directory, I need to process each image and generate an HTML file listing...
0
by: haylow | last post by:
Hi I am new to ASP.NET and am working on an application that runs on a webserver. The user will open up the web interface in a browser on their...
0
by: SampathTangudu | last post by:
Hi, We are trying to use the Hash Tables for passing information from one aspx page to another aspx page. We are using the below code. ...
0
by: Xah Lee | last post by:
Interactive Find and Replace String Patterns on Multiple Files Xah Lee, 2006-06 Suppose you need to do find and replace of a string pattern,...
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...
6
by: Mudcat | last post by:
Hi, I can't figure out why ctypes won't load the DLL I need to use. I've tried everything I can find (and the ctypes website is down at the...
1
by: Calvin Spealman | last post by:
I'm still on 10.4 and I'm trying to build pyOpenSSL, but I'm failing with Python.h trying to include and failing to find any of the shared...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.