User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 456,499 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,723 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 1537 | Replies: 5 | Solved
Reply
Join Date: Dec 2006
Posts: 21
Reputation: trihaitran is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
trihaitran trihaitran is offline Offline
Newbie Poster

Setting directory for opening files

  #1  
Dec 31st, 2006
This is a really simple question but it's been stumping me for a while. I want to open a file that is in the same directory as the .py script, but I want to specify that directory without using a universal path.

filepath1 = '/Users/username/Documents/Python/TC/TC cedict final.txt'
file = open(filepath1, 'rU')

Also an unrelated question is: If I make a program using wxpython into a .app for Macintosh or .exe for Windows, then does the person running it also have to have python and wxpython installed?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: Setting directory for opening files

  #2  
Jan 1st, 2007
I'm not sure if I'm reading your question correctly, but if you want to open a file that's in the SAME place as your .py script that opens the file, you don't need to set any form of path name. You just type in the name of the file on it's own, for example:

f = file("test.txt","r")
print f.read()

if you saved that into a folder, as long as the file: "test.txt" is in the same folder then it'd run fine.

As for your second question, i don't really know much about wxpython but as far as i was aware python programs were all interpreted, meaning they cant be made into 'stand-alone' .exe/.app programs, the only work around i know of is py2exe. If you used that then no the user wouldn't need to have python or wxpython installed, the downside to py2exe however is that it bundles all the dependencies together which means you might end up with a very large program folder.

Hope this helps
Reply With Quote  
Join Date: Dec 2006
Posts: 21
Reputation: trihaitran is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
trihaitran trihaitran is offline Offline
Newbie Poster

Re: Setting directory for opening files

  #3  
Jan 1st, 2007
I understand what you're saying, but that solution does not work. If I use the example as you have written then python looks in "/Users/username" for the file even if the script is not in there. What I'm curious to know is how to specify path relative to the location of the script.
Reply With Quote  
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: Setting directory for opening files

  #4  
Jan 1st, 2007
Hmm that's odd.

A single '.' represents the current working path. So you should in theory use that, e.g:
f = file("./SubFolder/test.txt", "r")
f.read()
the above code would open a file called test.txt in the SubFolder, which would be located in the same folder as the 'current working directory' (which SHOULD be where the script is).
So the full path for test.txt could be something like:

"Python/SubFolder/"

But I'm not sure why the previous example didn't work.
Anyway, try using a . in your path, that may work.

hope that helps
alex

P.S
By the way, a single dot (.) means the current folder,
a double dot (..) would go back one folder so you could use "../../" to go back two folders.

EDIT:
One more thing, http://www.bembry.org/technology/pyt.../lesson_12.php
has some pretty good info basic file/folder manipulation.
Last edited by a1eio : Jan 1st, 2007 at 8:34 pm. Reason: adding a useful link
Reply With Quote  
Join Date: Dec 2006
Posts: 21
Reputation: trihaitran is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
trihaitran trihaitran is offline Offline
Newbie Poster

Re: Setting directory for opening files

  #5  
Jan 1st, 2007
Somehow my current working directory is set to my home directory by default. I found a solution to this problem though.

__dir__ = os.path.dirname(os.path.abspath(__file__))
filepath = os.path.join(__dir__, 'test.txt')
file = open(filepath, 'rU')

This will get the absolute path to the directory of the *.py file and then I can add the name of my test.txt to it.
Last edited by trihaitran : Jan 1st, 2007 at 9:15 pm.
Reply With Quote  
Join Date: Aug 2005
Location: England - York
Posts: 136
Reputation: a1eio is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: Setting directory for opening files

  #6  
Jan 1st, 2007
ah that explains it, and that's a nice solution too, very useful, thanks
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 3:30 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC