By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 2,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

move up a level

P: n/a
alright, noob question: i want to open a file in a directory above the
current one i'm in. I'm trying to use myfile =
open('../thefileiwant.txt', 'r') but the ../ doesn't work. How do i
do it?

Nov 21 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
rp*****@yahoo.com wrote:
alright, noob question: i want to open a file in a directory above the
current one i'm in. I'm trying to use myfile =
open('../thefileiwant.txt', 'r') but the ../ doesn't work.
that's how it's usually done. what does "doesn't work" mean in this case?

</F>

Nov 21 '06 #2

P: n/a
rp*****@yahoo.com wrote:
alright, noob question: i want to open a file in a directory above the
current one i'm in. I'm trying to use myfile =
open('../thefileiwant.txt', 'r') but the ../ doesn't work. How do i
do it?
"Doesn't work" means what?
Have you looked at the getcwd and chdir functions in the os module?
You may wish to check that os.getcwd()'s notion of "current directory"
is the same as yours.
If you can't make any progress, ask again; do copy and paste the
relevant parts of your script instead of trying to describe what you
did. Also copy and paste output. Hint: use temporary print statements
to show what is happening.

HTH,
John

Nov 21 '06 #3

P: n/a
At Tuesday 21/11/2006 03:07, rp*****@yahoo.com wrote:
>alright, noob question: i want to open a file in a directory above the
current one i'm in. I'm trying to use myfile =
open('../thefileiwant.txt', 'r') but the ../ doesn't work. How do i
do it?
Above the process' current dir, or above the dir where the script resides?
You get the latter with 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
Nov 21 '06 #4

P: n/a
El Martes, 21 de Noviembre de 2006 03:07, rp*****@yahoo.com escribió:
alright, noob question: i want to open a file in a directory above the
current one i'm in. I'm trying to use myfile =
open('../thefileiwant.txt', 'r') but the ../ doesn't work. How do i
do it?
Hello, I proved your doubt in my PC and it works fine. This is the example:

Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02)
[GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>import os
os.getcwd()
'/home/manuel/descargas'
>>file = open('../proof.py')
print file.read()
import sys
if __name__ == '__main__':
print 'manuel'
>>>
--
Kaufmann Manuel
Nov 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.