python输出目录结构到excel



# -*- coding: utf-8 -*-
import xlwt

import os

wbk = xlwt.Workbook(encoding='utf-8')
sheet = wbk.add_sheet('tt')

j=0
def traverse(f):
    fs = os.listdir(f)
    global j
    for f1 in fs:
      tmp_path = os.path.join(f, f1)
      if not os.path.isdir(tmp_path):
          print('文件: %s' % tmp_path)
          pathlist = tmp_path.split('/')
          i = 0;
          j = j + 1
          for folderName in pathlist:

              if folderName == '':
                  print('空目录')
              else:
                  i = i+1


                  print(j,i,folderName)
                  savaFile(j, i, folderName)
      else:
          # print('文件夹:%s' % tmp_path)
          traverse(tmp_path)

def savaFile(v,h,content):
    sheet.write(v,h,content)

path = '/Users/yaokai/tizen-sdk/ide/configuration/org.eclipse.osgi/bundles'
traverse(path)
wbk.save('test.xls')




你可能感兴趣的:(python输出目录结构到excel)