[Q]How to append paths in a platform-independent way?

Peter Abel p-abel at t-online.de
Wed Apr 2 06:06:04 EST 2003


sdhyok at yahoo.com (sdhyok) wrote in message news:<420ced91.0304011946.56a22d2a at posting.google.com>...
> How to do the following jobs in a platform-independent way?
> 
> path = /home/sdhyok
> file = data.txt
> 
> 1. Append the file name to the path => /home/sdhyok/data.txt
> 2. Extract only path from /home/sdhyok/data.txt => /home/sdhyok
> 
> Daehyok

>>> import os
>>> # Because I work under MSW I took backslashes instead of slashes
>>> # But you can take os.sep, which is os-independant
>>> path=r'\home\sdhyok'
>>> #file is not a good name, because it is a builtin-function
>>> lfn='data.txt'
>>> path_file=os.path.join(path,lfn)
>>> print path_file
\home\sdhyok\data.txt
>>> print os.path.dirname(path_file)
\home\sdhyok

Regards Peter




More information about the Python-list mailing list