【问题标题】:What's wrong in python script?python脚本有什么问题?
【发布时间】:2011-10-23 06:03:32
【问题描述】:

python 脚本有什么问题?

代码:

import os    
import shutil
import getpass    
os.mkdir("C:\\dtmp") 

shutil.copy("C:\\path\\to\\bb-freeze-script.py","C:\\dtmp")
os.chdir("C:\\dtmp")
shutil.copy("C:\\path\\to\\main.py","C:\\dtmp")
os.system("python bb-freeze-script.py main.py")

os.mkdir("C:\\Program Files\\Directories v0.6")
os.chdir("C:\\")
shutil.rmtree("C:\\dtmp")

print getpass.getuser()

错误:

Traceback (most recent call last):
  File "bb-freeze-script.py", line 8, in <module>
    load_entry_point('bbfreeze==0.97.3', 'console_scripts', 'bb-freeze')()
  File "C:\Python27\lib\site-packages\bbfreeze-0.97.3-py2.7-win32.egg\bbfreeze\__init__.py", line 24, in main
    f.addScript(x)
  File "C:\Python27\lib\site-packages\bbfreeze-0.97.3-py2.7-win32.egg\bbfreeze\freezer.py", line 410, in addScript
    s = self.mf.run_script(path)
  File "C:\Python27\lib\site-packages\bbfreeze-0.97.3-py2.7-win32.egg\bbfreeze\modulegraph\modulegraph.py", line 241, in run_script
    co = compile(file(pathname, READ_MODE).read()+'\n', pathname, 'exec')
  File "C:\dtmp\main.py", line 14
    ^
IndentationError: expected an indented block

操作系统 -- Windows XP

【问题讨论】:

  • main.py 脚本中出现错误(缩进错误)。可以发一下代码吗?
  • 是的,这是 main.py 脚本中的错误
  • 代码: import os, sys class Directory(): def Create(self): dir = raw_input("Folder name: ") os.mkdir(dir) print "Created" def Delete(self) : dir = raw_input("文件夹名称:") os.rmdir(dir) print "Deleted" def exit():
  • 仅供参考,将冷冻机脚本编写为可从命令行调用的 Python 模块会更好/更好/更好,因为那时您可以import bb_freeze 并使用您将编写的 API那里。从 Python 代码调用 Python 代码实际上不应该通过 os.system 来完成。
  • 您不应在路径中使用反斜杠(正斜杠适用于包括 Windows 在内的所有系统)。如果你想保留它们,至少使用原始字符串 (´r'C:\something'´) 而不是转义狂欢 (´'C:\\something'´)

标签: python windows-xp main indentation


【解决方案1】:

以下是有关如何阅读回溯的快速演练。这很容易。

  • 查看您的代码,所有代码都在调用 Python 内置模块。可以肯定地说他们没有导致错误,所以唯一剩下的就是os.system 调用。果然,你通过所说的调用调用python(为什么不直接导入你想调用的模块?)。

  • 回溯确认错误出在您正在调用的其他 Python 中:

    Traceback (most recent call last):
      File "bb-freeze-script.py", line 8, in <module>
        load_entry_point('bbfreeze==0.97.3', 'console_scripts', 'bb-freeze')()
    
  • 接下来,通读脚本中的行,深入调用堆栈,找出错误发生的确切位置。

    File "C:\Python27\lib\site-packages\bbfreeze-0.97.3-py2.7-win32.egg\bbfreeze\__init__.py", line 24, in main
        f.addScript(x)
    File "C:\Python27\lib\site-packages\bbfreeze-0.97.3-py2.7-win32.egg\bbfreeze\freezer.py", line 410, in addScript
        s = self.mf.run_script(path)
    File "C:\Python27\lib\site-packages\bbfreeze-0.97.3-py2.7-win32.egg\bbfreeze\modulegraph\modulegraph.py", line 241, in run_script
        co = compile(file(pathname, READ_MODE).read()+'\n', pathname, 'exec')
    
  • 直到你到达​​p>

    File "C:\dtmp\main.py", line 14
    IndentationError: expected an indented block
    

你去,错误在main.py的第14行,你应该有一个缩进但没有。

【讨论】:

  • +1 用于教授如何钓鱼而不是简单地提供晚餐。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-16
  • 2015-04-06
相关资源
最近更新 更多