Take the 2-minute tour ×

I am experiencing an odd problem that I'm not quite sure how to tackle. I have a Python-selenium script that uses relative paths to log results to a text-file.

Here is the part of the script which sets up the log-file:

log_file = './demo-logfiles/log_file_template.txt'
sys.stdout = open('log_file_template.txt', 'a',)

As you can see, it uses a relative path to a folder. If I run this script as: python demo.py firefox MAC, it runs flawlessly and the logfile gets sent to the proper folder.

If I run this exact Python script from within a larger shell-script, it returns an error that the './demo-logfiles/log_file_template.txt' doesn't exist.

I have found that if I change the script to '../demo-logfiles/log_file_template.txt' it works in the larger shell script, but stops working if I run it normally.

It either works in one, or the other. What is the reason for the relative directories being interpreted in different ways? I would not like to have two separate scripts for running in Python/shell.

The original python script is in the directory /blah/blah/DEMO/demo.py, and the the shell script that runs it is in /blah/blah/DEMO/demo-autotest/autotest_logger.sh

I have confirmed that this problem occurs for any script I try to run. I shouldn't have to change the original Python code to make it work with the shell script. I already accounted for it in the shell script, and it successfully runs the file.


share|improve this question
How are you calling your script from the larger script? Does the other script live in a directory one level up? –  Justin Ethier Jan 23 '13 at 15:03
@JustinEthier refer to my edits. –  KamilS Jan 23 '13 at 15:09
add comment

2 Answers

up vote 3 down vote accepted

You should never use a "." (or any relative path) in a directory path in a script unless you really mean you want to refer to the directory that the user is running the script from. If you want to refer to a location relative to the script that's running, you can do the following:

import os
import sys
directory = os.path.dirname(os.path.abspath(__file__))
sys.stdout = open(os.path.join(directory, "demo-logfiles", "log_file_template.txt"), "a")

Best practices side note: you should probably use the logging module rather than reassigning sys.stdout.

share|improve this answer
Thank you, I appreciate the help! –  KamilS Jan 23 '13 at 15:43
add comment

The term "relative directories" means a path is relative to something. You probably assume it's relative to the script which contains the path but that's not correct.

Relative paths are relative to the current directory of the process which interprets the script, i.e. the folder in which you started python. If you're in the shell, you can see the current directory with echo $PWD

If you start python in /blah/blah, then that becomes the current directory and all relative paths are relative to /blah/blah.

See the answer of David Hollman for how to get the path of the current script and then how to build paths relative to that.

share|improve this answer
This clears things up for me, thank you. –  KamilS Jan 23 '13 at 15:44
add comment

Your Answer


