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?

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Edmund Carvalho
  • 107
  • 1
  • 3
  • 11

4 Answers4

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.

mikuszefski
  • 3,943
  • 1
  • 25
  • 38
  • 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 ? – Edmund Carvalho Nov 15 '17 at 10:00
  • While I am working on Linux I tested this on Win7 as your use of `G:\ ` suggested. – mikuszefski Nov 15 '17 at 10:02
  • 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. – Edmund Carvalho Nov 15 '17 at 10:05
  • 1
    @EdmundCarvalho No, the contrary; that's exactly why you'd use `os`. This code runs just as fine in Linux, no changes required. – mikuszefski Nov 15 '17 at 10:23
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')
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • 1
    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. – muad-dweeb Jul 28 '21 at 19:03
1

You can try this

import environ
environ.Path() - 1 + 'foo.txt'
Nagesh Dhope
  • 797
  • 5
  • 13
  • is `environ.Path()` equivalent to `environ.Path( __file__ )` ? – mikuszefski Nov 15 '17 at 08:57
  • No, not exactly. environ.Path() will give you path till current directory. This is give you output similar to pwd command in linux – Nagesh Dhope Nov 15 '17 at 10:55
  • **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 – AJ AJ Dec 14 '19 at 18:27
1

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

import os
os.path.abspath(os.path.join('..', os.getcwd()))
lk Arjun
  • 11
  • 3