python 批量创建文件

时间:2022-04-04 03:33:55

# coding:utf8

import os

path = "D:/Python_mkfile"

os.chdir(path)#切换到该目录

ysyl = u"验收文件"

file = open("num.txt", "r")

file2 = open("name.txt", "r")

num = file.readlines()

name = file2.readlines()

#当pycharm编码格式为utf8时,从文件中读取的中文打印在控制台是乱码,所以改为gbk(系统默认)

print name[3]

file.close()

file2.close()

#string.strip()去掉字符串前后的空格

#因为已经把pycharm编码格式改为了gbk,所以要把u"验收文件"编码格式改为GBK

for i in range(0, len(num)):

file3=open(ysyl.encode("GBK")+"-"+num[i].strip()+name[i].strip()+".xls", "w")

file3.close()