Skip to main content
Format code
Source Link
Stephen Rauch
  • 50.1k
  • 32
  • 119
  • 143

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

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

G###script path:\mainFolder\codeSrc\fun.py

G:\mainFolder\codeSrc\fun.py

G###desired file path:\mainFolder\foo.txt

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 I want to know if, is there a better (prettier :P) way to do this.?

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

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

G:\mainFolder\codeSrc\fun.py

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 I want to know if there a better (prettier :P) way to do this.

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?

Source Link

How to access file in parent directory using python?

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

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

G:\mainFolder\codeSrc\fun.py

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 I want to know if there a better (prettier :P) way to do this.