5

I am trying to access a text file in the parent directory,

Eg : python script is in codeSrc & the text file is in mainFolder.

script path:

G:\mainFolder\codeSrc\fun.py

desired file path:

G:\mainFolder\foo.txt

I am currently using this syntax with python 2.7x,

import os 
filename = os.path.dirname(os.getcwd())+"\\foo.txt"

Although this works fine, is there a better (prettier :P) way to do this?

5 Answers 5

9

While your example works, it is maybe not the nicest, neither is mine, probably. Anyhow, os.path.dirname() is probably meant for strings where the final part is already a filename. It uses os.path.split(), which provides an empty string if the path end with a slash. So this potentially can go wrong. Moreover, as you are already using os.path, I'd also use it to join paths, which then becomes even platform independent. I'd write

os.path.join( os.getcwd(), '..', 'foo.txt' )

...and concerning the readability of the code, here (as in the post using the environ module) it becomes evident immediately that you go one level up.

Sign up to request clarification or add additional context in comments.

4 Comments

I think i will go with the os.path.join method , as environ is not part of the python 2.7.13 package. . Have you tested the os.path.join method in a linux environment ?
While I am working on Linux I tested this on Win7 as your use of G:\ suggested.
Good guess :) , If I choose to port to a linux environment in the future do you see any drawbacks for the os.path.join method.
@EdmundCarvalho No, the contrary; that's exactly why you'd use os. This code runs just as fine in Linux, no changes required.
1

To get a path to a file in the parent directory of the current script you can do:

import os
file_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'foo.txt')

1 Comment

This is the one I needed because the file I am assembling the path from is nested lower than the active working directory. I can't make the assumption that execution is occurring in the same dir as the file.
1

You can try this

import environ
environ.Path() - 1 + 'foo.txt'

3 Comments

is environ.Path() equivalent to environ.Path( __file__ ) ?
No, not exactly. environ.Path() will give you path till current directory. This is give you output similar to pwd command in linux
package doesn't work with py 3.8 import environ File "C:\Users...\Python\Python38-32\lib\site-packages\environ.py", line 114 raise ValueError, "No frame marked with %s." % fname ^ SyntaxError: invalid syntax
1

to get the parent dir the below code will help you:

import os
os.path.abspath(os.path.join('..', os.getcwd()))

Comments

0

For anyone finding this using Python >=3.4 I found the following method the easiest

from pathlib import Path
filename = Path("../foo.txt").resolve()

https://docs.python.org/3/library/pathlib.html#pathlib.Path.resolve

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.