473,396 Members | 1,834 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,396 software developers and data experts.

How to open a txt file from the same folder as my module (w/outchanging the working dir)

I have a program in 'C:\Python25\Lib\site-packages\spam\spam.py'

Importing and everything works fine:
>>from spam import spam
But the program calls a file located on the same folder (that is:
C:\Python25\Lib\site-packages\spam\).

How do i do that?
>>spam.eggs()
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
datita = spam.eggs()
File "C:\Python25\lib\site-packages\spam\spam.py", line 149, in JustDoIt
config = open("configuration.txt", "rb").read().split('\r\n')
IOError: [Errno 2] No such file or directory: 'configuration.txt'

My last resort is to hard code the path for that file, but it's ugly,
and I want to know if I'm missing something. Am I?

Thanks a lot,
Sergio
Apr 4 '07 #1
3 1545
Sergio Correia wrote:
I have a program in 'C:\Python25\Lib\site-packages\spam\spam.py'

Importing and everything works fine:
>>>from spam import spam

But the program calls a file located on the same folder (that is:
C:\Python25\Lib\site-packages\spam\).

How do i do that?
>>>spam.eggs()

Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
datita = spam.eggs()
File "C:\Python25\lib\site-packages\spam\spam.py", line 149, in JustDoIt
config = open("configuration.txt", "rb").read().split('\r\n')
IOError: [Errno 2] No such file or directory: 'configuration.txt'

My last resort is to hard code the path for that file, but it's ugly,
and I want to know if I'm missing something. Am I?

Thanks a lot,
Sergio
The problem is that C:\Python25\Lib\site-packages\spam is not
the current working directory when you run the program. If it were,
and if configuration.txt is in that directory it WILL find it. If
you are running this from a shortcut make the working directory
C:\Python25\Lib\site-packages\spam

-Larry
Apr 4 '07 #2
En Wed, 04 Apr 2007 20:14:37 -0300, Larry Bates <la*********@websafe.com>
escribió:
Sergio Correia wrote:
>I have a program in 'C:\Python25\Lib\site-packages\spam\spam.py'

Importing and everything works fine:
>>>>from spam import spam

But the program calls a file located on the same folder (that is:
C:\Python25\Lib\site-packages\spam\).

How do i do that?

The problem is that C:\Python25\Lib\site-packages\spam is not
the current working directory when you run the program. If it were,
and if configuration.txt is in that directory it WILL find it. If
you are running this from a shortcut make the working directory
C:\Python25\Lib\site-packages\spam
If changing the working directory is not possible/convenient, use the
module __file__ attribute (spam.__file__) to obtain the directory where
spam.py resides.

--
Gabriel Genellina

Apr 5 '07 #3
Larry, Gabriel

Thanks for the replies. Both ways work great.

Sergio

On 4/4/07, Gabriel Genellina <ga*******@yahoo.com.arwrote:
En Wed, 04 Apr 2007 20:14:37 -0300, Larry Bates <la*********@websafe.com>
escribió:
Sergio Correia wrote:
I have a program in 'C:\Python25\Lib\site-packages\spam\spam.py'

Importing and everything works fine:
from spam import spam

But the program calls a file located on the same folder (that is:
C:\Python25\Lib\site-packages\spam\).

How do i do that?
The problem is that C:\Python25\Lib\site-packages\spam is not
the current working directory when you run the program. If it were,
and if configuration.txt is in that directory it WILL find it. If
you are running this from a shortcut make the working directory
C:\Python25\Lib\site-packages\spam

If changing the working directory is not possible/convenient, use the
module __file__ attribute (spam.__file__) to obtain the directory where
spam.py resides.

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list
Apr 5 '07 #4

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

Similar topics

5
by: Joerg Schuster | last post by:
Hello, I want to open the file 'configuration.smo' that is in directory dir. Yet, I don't know on which os my program is being run. On Unix I would say: f = open(dir + '/configuration.smo',...
4
by: Yuri Vorontsov | last post by:
Hallo! We have troubles (post XP SP2) to open local folders from the web application: - the web application allows users to select a local file (input type=file) - the system DOES NOT upload...
5
by: Phil Stanton | last post by:
Can anyone let me have the code for opening the open file dialog box and getting either a file or folder name as the output. I suspect it will look like Function GetPathOrFile(InputPath as...
4
by: santel_helvis | last post by:
Hi there, I wanna open the folder using asp.net. I am using anchor link to do that. But in href part how shall I give the location. Anyone plz help me out. Thanks in advance
9
by: Amit D.Shinde | last post by:
How to enable user for selecting only folders and not the files with the Open File Dialog. i.e. I want only path of the selected folder is to be returned by Open File Dialog. i.e Open File Dialog...
0
by: Aaron | last post by:
I have two imagebuttons in my asp.net (I'm using vb.net) application. I need one of them to open a folder and one to open a file within the aforementioned folder. I am not sure which class...
3
by: The Woo | last post by:
Can one programatically create a file folder which has the same name as a key field, using a command button? Or, Can there be a a command button which opens up a directory tree to a specified...
2
by: =?Utf-8?B?SmVmZnJleQ==?= | last post by:
I made a typo on a Project name (e.g. Wong, instead of Wang). Later on I renamed the Soution, Project, WebForm., etc, except the file folder name, back to Wang. Then after I closed the VS.net and...
0
by: Kurt Jakobsen | last post by:
Hello, I am having problem to download files via a bat script from an ftp server. When I do it manually (typing the commands into the ftp prog) it works. However this is no option as there are...
14
by: Ashutosh Bhawasinka | last post by:
Hi, How can I retrieve the system icon associated with a file/folder so that I can show it in the list view adjacent to the file/folder name? Regards, Ashutosh Bhawasinka
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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?
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.