Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some good ways to set a path in a Multi-OS supported Python script

Tags:

python

When writing a Python script that can be executed in different operating system environments (Windows/*nix), what are some good ways to set a path? In the example below I would like to have the logfiles stored in the logs folder under the current directory. Is this an acceptable approach (I'm rather new to Python) or are there better ways to achieve this? Thanks

if os.name == 'nt':
    logdir=('%s\\logs\\') % (os.getcwd())
else:
    logdir=('%s/logs/') % (os.getcwd())

logging.basicConfig(level=logging.INFO,
    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
    datefmt='%m-%d-%y %H:%M:%S',
    filename='%slogfile.log' % (logdir),
    filemode='a')
like image 673
Shaun Avatar asked May 09 '09 03:05

Shaun


People also ask

How do you set a path in Python?

To use it, you just pass a path or filename into a new Path() object using forward slashes and it handles the rest: Notice two things here: You should use forward slashes with pathlib functions. The Path() object will convert forward slashes into the correct kind of slash for the current operating system.

How do you write a file path in Python?

On Windows, paths are written using backslashes (\) as the separator between folder names. OS X and Linux, however, use the forward slash (/) as their path separator. If you want your programs to work on all operating systems, you will have to write your Python scripts to handle both cases.

What is Python OS path?

The os.path module is always the path module suitable for the operating system Python is running on, and therefore usable for local paths. However, you can also import and use the individual modules if you want to manipulate a path that is always in one of the different formats.

How do I give a Python path in Linux?

If you are using the standard flavour of Linux, open up the bash shell and type the following phrase, export PATH=”$PATH:/usr/local/bin/python” and press Enter. If you have access to either sh or ksh shell, then open up the terminal and type the following, PATH=”$PATH:/usr/local/bin/python” and press Enter.


2 Answers

Definitely have a look at os.path. It contains many of the "safe" cross-OS path manipulation functions you need. For example, I've always done this in the scenario you're outlining:

os.path.join(os.path.abspath(os.path.dirname(__file__)), 'logs')

Also note that if you want to get the path separator, you can use:

os.path.sep

This will yield '\\' on Windows, and '/' on Linux, for example.

like image 88
Ryan Duffield Avatar answered Sep 29 '22 05:09

Ryan Duffield


First, always use os.path for path manipulation.

More importantly, all paths should be provided in configuration files.

For logging, use the fileConfig function.

For everything else, be sure to have a configuration file or command-line parameter or environment variable.

like image 27
S.Lott Avatar answered Sep 29 '22 05:09

S.Lott