473,320 Members | 1,691 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,320 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 1742
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 this newsgroup and can't find anything like it...
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 os,sys © ©mydir= '/Users/t/web'
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 directory where the files are, in order to display their...
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 all of the images and links to the subdirectories....
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 local machine and enter a path to a directory. I...
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. IsolatedStorageFile isoStore =...
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, for all files in a directory. However, you do not...
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...
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 moment). Here's what I've seen so far. I've added...
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 libraries. I can't figure this one out because i just...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.