python - 更改代码后权限被拒绝
问题描述
我还是个新手,如果我搞砸了,很抱歉: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 基准测试的一些数据)。数据被写入一个excel文件。代码大部分都在工作。我仍然需要弄清楚如何正确地将数据输出到excel(格式不是我想要的)。但问题是,一旦我添加此代码,目录的权限就会改变:
def trigger_search(filename, line):
xyz = search_file(filename, line)
return xyz
一旦我添加了这个,我就会得到一个 errno 13: Permission denied。它不能再访问带有 .xml 文件的目录。即使我删除添加的代码,我仍然会收到此错误。唯一的解决方法是复制“未更改”代码(没有 trigger_search)并覆盖 .py 文件。只需复制粘贴,它就可以正常工作(无论我多久运行一次代码)。
任何提示为什么会发生这种情况以及如何解决这个问题?
请不要因为代码太苛刻,我知道这真的很像新手。它会在它工作后立即正确制作:D
解决方案
没关系的家伙。我只是个笨蛋。
在 trigger_search 中,我必须使用 search_folders,而不是 search_file。改回代码时,我也用 search_file 替换了它,尽管它之前在 main 方法中是 search_folders ..
使用正确的方法确实有效。我很抱歉...
推荐阅读
- oracle - 使用 Liquibase 创建 Oracle CTXSYS.CTXCAT 索引
- php - 从表格行提交 PHP
- azure-ad-graph-api - 无法使用 GraphAPI 通过电子邮件找到用户
- android - 如何在返回列表时将数据库中的日期转换为格式化字符串
- c# - NInject v3.3.4 和 WebApi 2,没有无参数构造函数
- python - 如何打印二维矩阵并添加标签,同时保持一切居中(Python)
- javascript - 使用 chromes 开发工具同时展开所有下拉链接?
- python - 在函数中定义一个类来中断装饰器的执行
- reactjs - 如何在本机反应中模拟上下文消费者反应元素
- html - 如何替换 JUST 域通配符而不是整行?