468,525 Members | 2,200 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,525 developers. It's quick & easy.

Can anyone explain the difference between results of append method

G'day all

Can anyone explain the difference in the results to me as I don't understand why specifying the directory name in two different ways could give a different answer.

In CODE 1 below i specified the directory path in the code.

In CODE 2 below i specified the directory path through a wxPython dialog.

When I run CODE 1 I get the following:

['d:\\temp,20070512_xfer_2_CA.pdf', 'd:\\temp,20070512_xfer_2_CC.pdf', 'd:\\temp,20070512_xfer_2_EA.pdf', 'd:\\temp,20070512_xfer_
2_HL.pdf', 'd:\\temp,4', 'd:\\temp,4dad', 'd:\\temp,aa', 'd:\\temp,access.en-us', 'd:\\temp,autorun.inf', 'd:\\temp,BARBIE_ISLAND_
PRINCESS.m4v', 'd:\\temp,cards1_1(1).xls', 'd:\\temp,excel.en-us', 'd:\\temp,F-7-i386-DVD.iso', 'd:\\temp,infopath.en-us', 'd:\\te
mp,Kristy_Tony.jpg', 'd:\\temp,mny2008usweb.exe', 'd:\\temp,Office 12.nfo', 'd:\\temp,office.en-us', 'd:\\temp,office.ww', 'd:\\te
mp,osetup.dll', 'd:\\temp,OsWalk.Show.txt', 'd:\\temp,outlook.en-us', 'd:\\temp,PB110027.JPG', 'd:\\temp,PB110028.JPG', 'd:\\temp,
PB110029.JPG', 'd:\\temp,powerpoint.en-us', 'd:\\temp,pro.ww', 'd:\\temp,ps3', 'd:\\temp,publisher.en-us', 'd:\\temp,setup.ww', 'd
:\\temp,SIS_CD_DVD copy.jpg', 'd:\\temp,SIS_CD_DVD.psd', 'd:\\temp,Start32.exe', 'd:\\temp,test.txt', 'd:\\temp,Thumbs.db', 'd:\\t
emp,torrents', 'd:\\temp,WebFE', 'd:\\temp,word.en-us', 'd:\\temp,Work', 'd:\\temp,ZbThumbnail.info', 'd:\\temp,[From www.metacafe
.com] 754577.4586323.11.flv']

When I run CODE 2 I get the following: (notice the u in front of all the list items)

[u'D:\\Temp,20070512_xfer_2_CA.pdf', u'D:\\Temp,20070512_xfer_2_CC.pdf', u'D:\\Temp,20070512_xfer_2_EA.pdf', u'D:\\Temp,20070512_x
fer_2_HL.pdf', u'D:\\Temp,4', u'D:\\Temp,4dad', u'D:\\Temp,aa', u'D:\\Temp,access.en-us', u'D:\\Temp,autorun.inf', u'D:\\Temp,BARB
IE_ISLAND_PRINCESS.m4v', u'D:\\Temp,cards1_1(1).xls', u'D:\\Temp,excel.en-us', u'D:\\Temp,F-7-i386-DVD.iso', u'D:\\Temp,infopath.e
n-us', u'D:\\Temp,Kristy_Tony.jpg', u'D:\\Temp,mny2008usweb.exe', u'D:\\Temp,Office 12.nfo', u'D:\\Temp,office.en-us', u'D:\\Temp,
office.ww', u'D:\\Temp,osetup.dll', u'D:\\Temp,OsWalk.Show.txt', u'D:\\Temp,outlook.en-us', u'D:\\Temp,PB110027.JPG', u'D:\\Temp,P
B110028.JPG', u'D:\\Temp,PB110029.JPG', u'D:\\Temp,powerpoint.en-us', u'D:\\Temp,pro.ww', u'D:\\Temp,ps3', u'D:\\Temp,publisher.en
-us', u'D:\\Temp,setup.ww', u'D:\\Temp,SIS_CD_DVD copy.jpg', u'D:\\Temp,SIS_CD_DVD.psd', u'D:\\Temp,Start32.exe', u'D:\\Temp,test.
txt', u'D:\\Temp,Thumbs.db', u'D:\\Temp,torrents', u'D:\\Temp,WebFE', u'D:\\Temp,word.en-us', u'D:\\Temp,Work', u'D:\\Temp,ZbThumb
nail.info', u'D:\\Temp,[From www.metacafe.com] 754577.4586323.11.flv']

Expand|Select|Wrap|Line Numbers
  1.  import os 
  2. def print_tree(dir_path):
  3.   outputList = []
  4.   for name in os.listdir(dir_path):
  5.     outputList.append(",".join([dir_path, name]))
  6.     print outputList
  7. print_tree(r"d:\temp")
Expand|Select|Wrap|Line Numbers
  1.  import os
  2. import wx 
  3. def print_tree(dir_path):
  4.   outputList = []
  5.   for name in os.listdir(dir_path):
  6.     outputList.append(",".join([dir_path, name]))
  7. print outputList 
  9. def dirchoose():
  10.   #'Gives the user selected path. Use: dirchoose()'
  11.   global _selectedDir , _userCancel #you should define them before
  12.   userPath = 'c:/'
  13.   app = wx.App()
  14.   dialog = wx.DirDialog(None, "Please choose your project directory:",\
  15.   style=1 ,defaultPath=userPath, pos = (5,10))
  16.   if dialog.ShowModal() == wx.ID_OK:
  17.     _selectedDir = dialog.GetPath()
  18.     return _selectedDir
  19.   else:
  20.     app.Close()
  21.     dialog.Destroy()
  22.     return _userCancel
  24. print_tree(dirchoose())
Jan 4 '08 #1
1 1766
2,851 Expert Mod 2GB
Apparently the wx method GetPath() returns a unicode string. os.listdir() will return directory entries in unicode if passed a path in unicode. Try this:
Expand|Select|Wrap|Line Numbers
  1. print_tree(str(dirchoose()))
Jan 4 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Greg Brunet | last post: by
19 posts views Thread by John Keeling | last post: by
4 posts views Thread by Bruno Alexandre | last post: by
18 posts views Thread by Jon Slaughter | last post: by
5 posts views Thread by Steven D'Aprano | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.