Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改代码后,权限被拒绝_Python_Permissions - Fatal编程技术网

Python 更改代码后,权限被拒绝

Python 更改代码后,权限被拒绝,python,permissions,Python,Permissions,我还是个新手,如果我搞砸了,我很抱歉:D 因此,我试图编写一个脚本,通过一些.xml文件来获得某些行,并将它们放入excel工作表中 代码: 我一加上这个就得到一个错误13:权限被拒绝。 它无法再使用.xml文件访问目录。 即使我删除了添加的代码,我仍然会得到这个错误。 唯一的解决方法是复制“未更改”代码(不使用触发器搜索)并覆盖.py文件。 只要复制粘贴就可以了(不管我多久运行一次代码) 有没有提示为什么会发生这种情况以及如何解决 请不要因为代码太苛刻,我知道这真的很像新手。它一工作就可以正常

我还是个新手,如果我搞砸了,我很抱歉:D

因此,我试图编写一个脚本,通过一些.xml文件来获得某些行,并将它们放入excel工作表中

代码:

我一加上这个就得到一个错误13:权限被拒绝。 它无法再使用.xml文件访问目录。 即使我删除了添加的代码,我仍然会得到这个错误。 唯一的解决方法是复制“未更改”代码(不使用触发器搜索)并覆盖.py文件。 只要复制粘贴就可以了(不管我多久运行一次代码)

有没有提示为什么会发生这种情况以及如何解决


请不要因为代码太苛刻,我知道这真的很像新手。它一工作就可以正常运行:D

这里它返回错误的原因是因为您没有以管理员身份运行它。在我向您演示如何解决此问题之前,您需要成为计算机的管理员,或者必须知道管理员的密码。此外,此答案假设您正在计算机上运行Windows

如果从CMD运行此程序,则有两种方法

  • 有两种方法可以做到这一点。第一种方法是运行程序“run”(由Windows自动安装),然后键入cmd.exe。第二种方法是轻触Windows键并查找cmd,右键单击显示“命令提示符”的按钮,然后单击显示“以管理员身份运行”的按钮
  • 第二种方法是打开命令提示符,输入runas/profile/user:administrator“insert\path\here\program.py”,其中“administrator”应替换为计算机上的用户名,“insert\path\here”应替换为程序所在的路径,以及“program.py”应替换为程序名(如果与程序位于同一目录中,则不需要包含路径)
1如果您从WINDOWS搜索栏运行此程序

  • 在Windows搜索栏(左下角)中,向上搜索程序的文件名(例如,“program.py”),然后右键单击它并单击显示“以管理员身份运行”的按钮
    • 别担心,伙计们。 我只是个哑巴

      在触发器搜索中,我必须使用搜索文件夹,而不是搜索文件。 当更改回代码时,我还将其替换为search_文件,尽管在main方法中它以前是search_文件夹

      使用正确的方法确实有效。
      非常抱歉……

      您是否确实查看过许可证?如果是,您可以在运行脚本之前和之后发布它们。您从未关闭过该文件。尝试关闭它。目录上的权限设置为写保护。我一次又一次地关闭它,但它再次显示为受保护。但正如我所说,如果代码保持不变,它仍然可以访问它。该文件一直处于关闭状态,我知道如果它处于打开状态,它将无法访问它。从trigger_search添加代码不会触发错误。只有当我将main方法:output=search\u file(文件名,行)中的代码更改为output=trigger\u search(文件名,行)时,我才会得到错误。如果我将其切换回原来的位置,仍然会出现错误,即使删除触发器搜索中的所有内容也无法修复错误。我必须复制粘贴代码来修复它。这对我来说毫无意义。如果我删除trigger_search,它的代码与copy pastedHi完全相同,谢谢你的回答。我是计算机的管理员^^代码实际上可以访问该文件,而无需以特权运行它。就像这样:代码:abc很好用。只要我喜欢:代码:a d b c,它就不能再访问它了。删除D不会改变任何事情。我必须再次复制粘贴代码(与出现此错误时完全相同)。我不会关闭.py或任何东西。我只是用ABC覆盖ABC,它又能工作了。。。编辑:我和pycharm一起工作
      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()
      
      def trigger_search(filename, line):
          xyz = search_file(filename, line)
          return xyz