import os
import xlwt  # 操作excel模块
import sys

file_path = sys.path[0] + '\\filename.xls'  # sys.path[0]为要获取当前路径,filename为要写入的文件
f = xlwt.Workbook(encoding='utf-8', style_compression=0)  # 新建一个excel
sheet = f.add_sheet('sheet1')  # 新建一个sheet
pathDir = os.listdir(r'\\10.99.11.6\data\RenameModels\1\1ALLY')  # 文件放置在当前文件夹中,用来获取当前文件夹内所有文件目录

i = 0  # 将文件列表写入test.xls
for s in pathDir:
    sheet.write(i, 0, r'\\10.99.11.6\data\RenameModels\1\1ALLY'+'\\'+s)
    sheet.write(i, 1, s)
    i = i + 1

print(file_path)
print(i)  # 显示文件名数量
f.save(file_path)

 

 


import os
#输出数据写入CSV文件
import csv
# data = [
#     ("Mike", "male", 24),
#     ("Lee", "male", 26),
#     ("Joy", "female", 22)
# ]

pathDir = os.listdir(r'\\10.99.11.6\data\RenameModels\1\1ALLY')  # 文件放置在当前文件夹中,用来获取当前文件夹内所有文件目录

#Python3.4以后的新方式,解决空行问题
with open('d://filename.csv', 'w', newline='') as csv_file:
    csv_writer = csv.writer(csv_file)
    # for list in data:
    #     print(list)
    for s in pathDir:
        data = [r'\\10.99.11.6\data\RenameModels\1\1ALLY'+"\\"+s, s]
        csv_writer.writerow(data)

 

 

posted on 2020-09-14 16:07  ing学习ing  阅读(1009)  评论(0编辑  收藏  举报