[Tutor] listdir, ispath and unicode (followup question)

Jeff Shannon jeff@ccvcorp.com
Wed, 31 Jul 2002 14:30:48 -0700


Poor Yorick wrote:

> filename = os.path.join(os.getcwd(), os.listdir(os.getcwd())[0])

This is tangential to your real problem, but the above snippet can be made a little
clearer by using os.path.abspath(), which combines relative path information with
the current working directory to calculate an absolute path.

>>> os.chdir('c:/temp')
>>> os.listdir(os.getcwd())[0]
'textfile.txt'
>>> os.path.abspath('textfile.txt')
'c:\\temp\\textfile.txt'
>>>

Combined all together, you would use

filename = os.path.abspath( os.listdir(os.getcwd())[0] )

Still not exactly transparent, but a little less ugly than the os.path.join()
solution.  :)

Jeff Shannon
Technician/Programmer
Credit International