有人能解释append方法的结果之间的区别吗

g'day全部
任何人都可以向我解释结果的差异,因为我不明白为什么以两种不同的方式指定目录名称可以给出不同的答案.
在下面的代码1中,我指定了代码中的目录路径.
在下面的代码2中,我通过WXPYTHON对话框指定了目录路径.
当我运行代码1时,我会得到以下内容:
['d:\\ temp,20070512_xfer_2_ca.pdf','d:\\ temp,20070512_xfer_2_2_cc.pdf','d:\\ temp,20070512_xfer_2_2_ea.pdf'
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,card1_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:d: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:d:d:d:d:d:d:d:d:d:d:d:d:d: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,[来自

www.metacafe

.com] 754577.4586323.11.flv']
当我运行代码2时,我会得到以下内容:
(注意所有列表项目前的U)
[u'd:\\ temp,20070512_xfer_2_ca.pdf',u'd:\\ temp,20070512_xfer_2_2_2_cc.pdf',u'd:\\ temp,20070512_xfer_2_2_2_ea.pdf'.pdf',U'D:
fer_2_hl.pdf',u'd:\\ temp,4',u'd:\\ temp,4dad',u'd:\\ temp,aa',u'd:\\ temp,access.en--我们',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,u' \ temp,office.en-us',u'd:\\ temp,
office.ww',u'd:\\ temp,osetup.dll',u'd:\\ temp,oswalk.show.txt',u'd:\\ temp,ueutlook.en-us',u',u',u' d:\\ temp,pb110027.jpg',u'd:\\ temp,p,p
b110028.jpg',u'd:\\ temp,pb110029.jpg',u'd:\\ temp,powerpoint.en-us',u'd:\\ temp,pro.ww',u'd:u'd:u'd:u'd: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--我们',u'd:\\ temp,work',u'd:\\ temp,zbthumb
nail.info',u'd:\\ temp,[来自

www.metacafe.com

] 754577.4586323.11.flv']
代码1:

选择 | 换行 | 行号
  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")
  8.  

代码2:

选择 | 换行 | 行号
  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 
  8.  
  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
  23.  
  24. print_tree(dirchoose())
  25.  
# 回答1


显然,wx方法getPath()返回一个Unicode字符串.
os.listdir()
如果通过Unicode中的路径,将返回Unicode中的目录条目.尝试这个:

选择 | 换行 | 行号
  1. print_tree(str(dirchoose()))

标签: python

添加新评论