天天上头条 发表于 2017-10-19 13:18:52

求解释两端代码

代码:
list_ff=os.listdir(os.curdir)
type_dic={}
for each_file in list_ff:
    if os.path.isdir(each_file):
      type_dic.setdefault('文件夹',0)
      type_dic['文件夹']+=1
    else:
      ext=os.path.splitext(each_file)
      type_dic.setdefault(ext,0)
      type_dic+=1
for each_type in type_dic.keys():
    print('改文件夹下共有类型为[%s]的文件%d个'%(each_type,type_dic))


求解释:
type_dic['文件夹']+=1
ext=os.path.splitext(each_file)
对于这两行代码不理解,求大神解释下

新手·ing 发表于 2017-10-21 22:10:29

第一行那个:从字典里找到文件夹这个key给它的value+1
第二行:
你要理解这个函数的用法,
看一下代码:
os.path.splitext(path)
分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作
>>> os.path.splitext('c:\\csv\\test.csv')
('c:\\csv\\test', '.csv')
然后就这样把each_file分离得到第二个值(切片)
就是文件的后缀名并赋值给ext

天天上头条 发表于 2017-10-28 16:08:04

新手·ing 发表于 2017-10-21 22:10
第一行那个:从字典里找到文件夹这个key给它的value+1
第二行:
你要理解这个函数的用法,


谢谢

新手·ing 发表于 2017-10-28 21:43:16

天天上头条 发表于 2017-10-28 16:08
谢谢

下次记得点最佳答案
页: [1]
查看完整版本: 求解释两端代码