我是Python的新手,我遇到了以下脚本的问题:
#modules
import time
import os
import xlsxwriter
#the path for where all the things the progrem these should happen
PATH = r"C:\status\status"
#gives you the date and time used for the name of the mother file
DATE = time.strftime('%d,%m')
#creatss a file for xls
def xl():
xl = cyberboad = os.makedirs(r"C:\status\status" + '\\' + DATE + '\\' + "XL")
#creatss the file tree for cyberboard with one file for ech opration and xl workbooks in ech file with oprations file
def cyberboad():
cyberboad = os.makedirs(r"C:\status\status" + '\\' + DATE + '\\' + "CYBERBOARD")
#operations
operation1010 = os.makedirs(r"C:\status\status" + '\\' + DATE + '\\' + "CYBERBOARD" + '\\' + "1010")
os.chdir(r"C:\status\status" + '\\' + DATE + '\\' + "CYBERBOARD" + '\\' + "1010")
workbook = xlsxwriter.Workbook('mazehim.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
worksheet.write('A1', 'getfile', bold)
worksheet.write('C1', 'dir', bold)
operation90247 = os.makedirs(r"C:\status\status" + '\\' + DATE + '\\' + "CYBERBOARD" + '\\' + "90247")
#creates the xl workbook for ech file and writes in it get file,dir at the top of colum A,C
os.chdir(r"C:\status\status" + '\\' + DATE + '\\' + "CYBERBOARD" + '\\' + "90247")
workbook = xlsxwriter.Workbook('mazehim.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
worksheet.write('A1', 'getfile', bold)
worksheet.write('C1', 'dir', bold)
operation9969 = os.makedirs(r"C:\status\status" + '\\' + DATE + '\\' + "CYBERBOARD" + '\\' + "9969")
os.chdir(r"C:\status\status" + '\\' + DATE + '\\' + "CYBERBOARD" + '\\' + "9969")
workbook = xlsxwriter.Workbook('mazehim.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
worksheet.write('A1', 'getfile', bold)
worksheet.write('C1', 'dir', bold)
operation90665 = os.makedirs(r"C:\status\status" + '\\' + DATE + '\\' + "CYBERBOARD" + '\\' + "90665")
os.chdir(r"C:\status\status" + '\\' + DATE + '\\' + "CYBERBOARD" + '\\' + "90665")
workbook = xlsxwriter.Workbook('mazehim.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
worksheet.write('A1', 'getfile', bold)
worksheet.write('C1', 'dir', bold)
operationmatchupzone = os.makedirs(r"C:\status\status" + '\\' + DATE + '\\' + "CYBERBOARD" + '\\' + "matchupzone")
os.chdir(r"C:\status\status" + '\\' + DATE + '\\' + "CYBERBOARD" + '\\' + "matchupzone")
workbook = xlsxwriter.Workbook('mazehim.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
worksheet.write('A1', 'getfile', bold)
worksheet.write('C1', 'dir', bold)
#creatss the file tree for allrivers with one file for ech opration and xl workbooks in ech file with oprations file
def allrivers():
allrivers = os.makedirs(r"C:\status\status" + '\\' + DATE + '\\' + "ALLRIVERS")
#operations
operation5553 = os.makedirs(r"C:\status\status" + '\\' + DATE + '\\' + "ALLRIVERS" + '\\' + "5553")
os.chdir(r"C:\status\status" + '\\' + DATE + '\\' + "ALLRIVERS" + '\\' + "5553")
workbook = xlsxwriter.Workbook('mazehim.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
worksheet.write('A1', 'getfile', bold)
worksheet.write('C1', 'dir', bold)
operation8256 = os.makedirs(r"C:\status\status" + '\\' + DATE + '\\' + "ALLRIVERS" + '\\' + "8256")
os.chdir(r"C:\status\status" + '\\' + DATE + '\\' + "ALLRIVERS" + '\\' + "8256")
workbook = xlsxwriter.Workbook(' mazehim.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
worksheet.write('A1', 'getfile', bold)
worksheet.write('C1', 'dir', bold)
operationeberkiss = os.makedirs(r"C:\status\status" + '\\' + DATE + '\\' + "ALLRIVERS" + '\\' + "emberkiss")
os.chdir(r"C:\status\status" + '\\' + DATE + '\\' + "ALLRIVERS" + '\\' + "emberkiss")
workbook = xlsxwriter.Workbook('mazehim.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
worksheet.write('A1', 'getfile', bold)
worksheet.write('C1', 'dir', bold)
operationlaguardia = os.makedirs(r"C:\status\status" + '\\' + DATE + '\\' + "ALLRIVERS" + '\\' + "laguardia")
os.chdir(r"C:\status\status" + '\\' + DATE + '\\' + "ALLRIVERS" + '\\' + "laguardia")
workbook = xlsxwriter.Workbook('mazehim.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
worksheet.write('A1', 'getfile', bold)
worksheet.write('C1', 'dir', bold)
operation1024 = os.makedirs(r"C:\status\status" + '\\' + DATE + '\\' + "ALLRIVERS" + '\\' + "1024")
os.chdir(r"C:\status\status" + '\\' + DATE + '\\' + "ALLRIVERS" + '\\' + "1024")
workbook = xlsxwriter.Workbook('mazehim.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
worksheet.write('A1', 'getfile', bold)
worksheet.write('C1', 'dir', bold)
#calling the fenctions set above
cyberboad()
allrivers()
xl()
我知道它不是一个非常干净的脚本,它的作用是创建一个文件夹,然后在该文件夹中使用模块xlsxwriter
创建一个Excel文件。
它工作正常,但有时却没有。我做错了什么?
谢谢。
答案 0 :(得分:1)
它可以是很多东西,并且很难分辨出代码是以这种方式编写的。但是我注意到了几个问题:
workbook
。您应该创建一次。并使用相同的工作簿对象workbook.close()
)xl
是否符合你的意思。它只会创造一个" XL"夹请尝试简化代码并更好地解释出错的原因 你可以尝试这个简化的代码:
import time
import os
import xlsxwriter
#the path for where all the things the progrem these should happen
PATH = r"C:\status\status"
#gives you the date and time used for the name of the mother file
DATE = time.strftime('%d,%m')
workbook = xlsxwriter.Workbook('mazehim.xlsx')
bold = workbook.add_format({'bold': True})
#creatss a file for xls
def xl():
os.makedirs(PATH + '\\' + DATE + '\\' + "XL")
#creatss the file tree for cyberboard with one file for ech opration and xl workbooks in ech file with oprations file
def cyberboad():
cyberboad = os.makedirs(r"C:\status\status" + '\\' + DATE + '\\' + "CYBERBOARD")
#operations
for f in ["1010","90247","9969","90665","matchupzone"]:
os.makedirs(PATH + '\\' + DATE + '\\' + "CYBERBOARD" + '\\' + f)
os.chdir(PATH + '\\' + DATE + '\\' + "CYBERBOARD" + '\\' + f)
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'getfile', bold)
worksheet.write('C1', 'dir', bold)
#creatss the file tree for allrivers with one file for ech opration and xl workbooks in ech file with oprations file
def allrivers():
allrivers = os.makedirs(r"C:\status\status" + '\\' + DATE + '\\' + "ALLRIVERS")
#operations
for f in ["5553","8256","emberkiss","laguardia","1024"]:
os.makedirs(PATH + '\\' + DATE + '\\' + "ALLRIVERS" + '\\' + f)
os.chdir(PATH + '\\' + DATE + '\\' + "ALLRIVERS" + '\\' + f)
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'getfile', bold)
worksheet.write('C1', 'dir', bold)
#calling the fenctions set above
cyberboad()
allrivers()
xl()
workbook.close()
答案 1 :(得分:0)
import time
import os
import xlsxwriter
#the path for where all the things the progrem these should happen
PATH = r"C:\status\status"
#gives you the date and time used for the name of the mother file
DATE = time.strftime('%d,%m')
#creatss a file for xls
def xl():
os.makedirs(PATH + '\\' + DATE + '\\' + "XL")
#creatss the file tree for cyberboard with one file for ech opration and xl workbooks in ech file with oprations file
def cyberboad():
cyberboad = os.makedirs(r"C:\status\status" + '\\' + DATE + '\\' + "CYBERBOARD")
#operations
for f in ["1010","90247","9969","90665","matchupzone"]:
os.makedirs(PATH + '\\' + DATE + '\\' + "CYBERBOARD" + '\\' + f)
os.chdir(PATH + '\\' + DATE + '\\' + "CYBERBOARD" + '\\' + f)
workbook = xlsxwriter.Workbook('mazehim.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
worksheet.write('A1', 'getfile', bold)
worksheet.write('C1', 'dir', bold)
workbook.close()
#creatss the file tree for allrivers with one file for ech opration and xl workbooks in ech file with oprations file
def allrivers():
allrivers = os.makedirs(r"C:\status\status" + '\\' + DATE + '\\' + "ALLRIVERS")
#operations
for f in ["5553","8256","emberkiss","laguardia","1024"]:
os.makedirs(PATH + '\\' + DATE + '\\' + "ALLRIVERS" + '\\' + f)
os.chdir(PATH + '\\' + DATE + '\\' + "ALLRIVERS" + '\\' + f)
workbook = xlsxwriter.Workbook('mazehim.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})
worksheet.write('A1', 'getfile', bold)
worksheet.write('C1', 'dir', bold)
workbook.close()
#calling the fenctions set above
cyberboad()
allrivers()
xl()
嘿,只是想让你知道代码现在正常运行,并感谢它的帮助,祝你有个美好的一天 当然上面的代码是成品