我还是这个新手,如果我搞砸了,那么抱歉:D
因此,我正在尝试编写一个脚本,该脚本会遍历一些.xml文件以获取某些行并将其放入Excel工作表中。
代码:
import os
import openpyxl
def main():
print_header()
folder = get_folder_from_user()
if not folder:
print("Sorry, that folder doesn't exist.")
return
text = get_search_text_from_user()
if not text:
print("Sorry, I can't search for nothing.")
return
count = input("Show match count? y/n")
name = "output.xlsx"
# name = input("Name for workbook: ")
x = []
output = search_file(folder, text)
match_count = 0
for i in output:
match_count += 1
string = i
string = string.split(">")
string = string[1]
string = string.split("<")
string = string[0]
i = string
print(i)
x.extend([i])
write_to_workbook(name, x)
if count == "y":
print("==================")
print("Found {} matches".format(match_count))
print("==================")
def write_to_workbook(name, x):
wb = openpyxl.Workbook()
ws = wb.active
a = 1
ws.append(x)
wb.save("C:/Users/Kevin/Desktop/{}".format(name))
a += 1
def print_header():
print("-----------------------------------")
print("----------File Search App----------")
print("-----------------------------------")
print()
def get_folder_from_user():
folder = input("Which folder do you want to search? ")
if not folder or not folder.strip():
return None
if not os.path.isdir(folder):
return None
return os.path.abspath(folder)
def get_search_text_from_user():
print("Which data do you want me to copy for you?")
print("[1]SeqTest Read")
print("[2]SeqTest Write")
print("[3]Random 4K1TTest Read")
print("[4]Random 4K1TTest Write")
print("[5]Random 4K64TTest Read")
print("[6]Random 4K64TTest Write")
print("[7]AccTimeTest Read")
print("[8]AccTimeTest Write")
print("[9]Score")
print("[0]All")
choice = int(input("Choose now: "))
if choice == 1:
line = 15
elif choice == 2:
line = 16
elif choice == 3:
line = 19
elif choice == 4:
line = 20
elif choice == 5:
line = 23
elif choice == 6:
line = 24
elif choice == 7:
line = 27
elif choice == 8:
line = 28
elif choice == 9:
line = 99
elif choice == 0:
line = 100
else:
line = 0
line = 15
return int(line)
def search_folders(folder, line):
items = os.listdir(folder)
for item in items:
full_item = os.path.join(folder, item)
if os.path.isdir(full_item):
yield from search_folders(full_item, line)
else:
yield from search_file(full_item, line)
def search_file(filename, line):
with open(filename, 'r', encoding='utf-8') as fin:
lines = fin.readlines()
if line == 99:
print(lines[31])
print(lines[32])
print(lines[33])
yield ("/n".join(lines[31:34]))
elif line == 100:
s = 0
while s < 10:
print(filename)
print(lines[4])
if line == 15 or 16:
print("Seq")
if line == 15:
print("Read")
else:
print("Write")
elif line == 19 or 20:
print("4k ")
if line == 19:
print("Read")
else:
print("Write")
elif line == 23 or 24:
print("4k 64")
if line == 23:
print("Read")
else:
print("Write")
elif line == 27 or 28:
print("Acc")
if line == 27:
print("Read")
else:
print("Write")
elif line == 99:
print("")
yield (lines[line])
else:
print(filename)
print(lines[4])
if line == 15 or 16:
print("Seq")
if line == 15:
print("Read")
else:
print("Write")
elif line == 19 or 20:
print("4k ")
if line == 19:
print("Read")
else:
print("Write")
elif line == 23 or 24:
print("4k 64")
if line == 23:
print("Read")
else:
print("Write")
elif line == 27 or 28:
print("Acc")
if line == 27:
print("Read")
else:
print("Write")
elif line == 99:
print("")
yield (lines[line])
if __name__ == '__main__':
main()
简而言之: 用户必须输入带有文本文件的目录。 然后选择所需的行。 (已固定在第15行进行测试。当我想要选择中指定的每一行时,我并没有解决问题。 然后询问用户他是否想要匹配的总数。 然后,它遍历所有文本文件并输出每个文件的第15行(仅来自ssd基准测试的一些数据)。 将数据denn写入excel文件。 该代码主要在工作。我仍然必须弄清楚如何正确地将数据输出到excel(格式不是我想要的格式)。 但是问题是,一旦添加此代码,该目录的权限就会更改:
def trigger_search(filename, line):
xyz = search_file(filename, line)
return xyz
添加此内容后,我会看到错误号13:权限被拒绝。 它不再可以使用.xml文件访问目录。 即使删除添加的代码,仍然会出现此错误。 唯一的解决方法是复制“未更改”的代码(不使用trigger_search)并覆盖.py文件。 只需复制粘贴就可以了(无论我多久运行一次代码)。
任何提示为何会发生这种情况以及如何解决此问题?
请不要因为代码太苛刻,我知道这确实是新手。它会在工作后立即正确制作:D
答案 0 :(得分:1)
没关系的家伙。 我只是愚蠢。
在trigger_search中,我必须使用search_folders,而不是search_file。 当改回代码时,我也用search_file替换了它,尽管在main方法中以前是search_folders。
使用正确的方法实际上有效。 很抱歉...
答案 1 :(得分:0)
此处返回错误的原因是,您尚未以管理员身份运行它。在向您展示如何解决问题之前,您将需要成为计算机上的管理员,或者必须知道管理员的密码。另外,此答案假设您在计算机上运行Windows。
如果您通过CMD运行此程序,有2种方法
从WINDOWS搜索栏中运行此程序的一种方式