Python 如果我在断点处停止,异常将消失

Python 如果我在断点处停止,异常将消失,python,Python,Python 3.6.2 下面代码的问题是,在运行时会引发异常。 但在调试器中,它可以完美地工作。我在调试器中停止的位置在注释中标记为断点 我在IDE和shell中都尝试了该命令。出现例外情况。因此,这个问题与IDE无关 这种情况使我有点震惊。 我拍了一段视频: 你能在这里踢我一脚吗?怎么可能呢? 对下面的代码进行评论(与问题无关,只是出于好奇) 这是一个用于Django web框架的实用程序。 用户上传文件后,会将其放入媒体目录 当然,Django知道媒体目录的位置。 然后Django在数据库

Python 3.6.2

下面代码的问题是,在运行时会引发异常。 但在调试器中,它可以完美地工作。我在调试器中停止的位置在注释中标记为断点

我在IDE和shell中都尝试了该命令。出现例外情况。因此,这个问题与IDE无关

这种情况使我有点震惊。 我拍了一段视频:

你能在这里踢我一脚吗?怎么可能呢?

对下面的代码进行评论(与问题无关,只是出于好奇)

这是一个用于Django web框架的实用程序。 用户上传文件后,会将其放入媒体目录

当然,Django知道媒体目录的位置。 然后Django在数据库中保留与媒体相关的路径。大概是这样的:

  • it_1/705fad82-2f68-4f3c-90c2-116DA3AD9A4.txt'

  • e5474da0-0fd3-4fa4-a85f-15c767ac32d4.djvu

我想确切地知道保存在媒体中的文件对应于数据库中的路径。没有额外的文件,没有短缺

代码:

from pathlib import Path

class <Something>():
    def _reveal_lack_extra_files(self):

        path = os.path.join(settings.BASE_DIR, '../media/')

        image_files = Image.objects.values_list("file", flat=True)
        image_files = [Path(os.path.join(path, file)) for file in image_files]

        item_files = ItemFile.objects.values_list("file", flat=True)
        item_files = [Path(os.path.join(path, file)) for file in item_files]

        sheet_files = SheetFile.objects.values_list("file", flat=True)
        sheet_files = [Path(os.path.join(path, file)) for file in sheet_files]

        expected_files = set().union(image_files, item_files, sheet_files)

        real_files = set()

        glob_generator = list(Path(path).glob("**/*"))

        for posix_path in glob_generator:
            if os.path.isfile(posix_path._str): # Breakpoint
                real_files.add(posix_path)


        lack = expected_files.difference(real_files)
        extra = real_files.difference(expected_files)

        assert bool(lack) == False, "Lack of files: {}".format(lack)
        assert bool(extra) == False, "Extra files: {}".format(extra)
/home/michael/PycharmProjects/venv/photoarchive_4/bin/python /home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/pydevd.py --multiproc --qt-support --client 127.0.0.1 --port 43849 --file /home/michael/PycharmProjects/photoarchive_4/manage.py checkfiles
warning: Debugger speedups using cython not found. Run '"/home/michael/PycharmProjects/venv/photoarchive_4/bin/python" "/home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/setup_cython.py" build_ext --inplace' to build.
pydev debugger: process 3840 is connecting

Connected to pydev debugger (build 171.4694.67)
Traceback (most recent call last):
  File "/home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/pydevd.py", line 1591, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/pydevd.py", line 1018, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/michael/PycharmProjects/photoarchive_4/manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/michael/PycharmProjects/photoarchive_4/general/management/commands/checkfiles.py", line 59, in handle
    self._reveal_lack_extra_files()
  File "/home/michael/PycharmProjects/photoarchive_4/general/management/commands/checkfiles.py", line 39, in _reveal_lack_extra_files
    if os.path.isfile(posix_path._str):
AttributeError: _str

Process finished with exit code 1
从pathlib导入路径
类():
def(显示)缺少(额外)文件(自身):
path=os.path.join(settings.BASE_DIR,../media/)
image\u files=image.objects.values\u list(“file”,flat=True)
image\u files=[Path(os.Path.join(Path,file))用于image\u文件中的文件]
item_files=ItemFile.objects.values_list(“file”,flat=True)
item_files=[Path(os.Path.join(Path,file))用于item_文件中的文件]
图纸文件=图纸文件.objects.values\u列表(“文件”,flat=True)
工作表文件=[工作表文件中文件的路径(os.Path.join(Path,file))]
应为\u files=set().union(图像\u文件、项目\u文件、图纸\u文件)
real_files=set()
glob_generator=list(路径).glob(***)
对于全局生成器中的posix_路径:
如果os.path.isfile(posix_path._str):#断点
real_files.add(posix_路径)
缺少=预期的\u文件。差异(实际\u文件)
extra=实际的\u文件。差异(预期的\u文件)
assert bool(lack)=False,“缺少文件:{}”。格式(lack)
断言bool(extra)=False,“额外文件:{}”。格式(extra)
回溯:

from pathlib import Path

class <Something>():
    def _reveal_lack_extra_files(self):

        path = os.path.join(settings.BASE_DIR, '../media/')

        image_files = Image.objects.values_list("file", flat=True)
        image_files = [Path(os.path.join(path, file)) for file in image_files]

        item_files = ItemFile.objects.values_list("file", flat=True)
        item_files = [Path(os.path.join(path, file)) for file in item_files]

        sheet_files = SheetFile.objects.values_list("file", flat=True)
        sheet_files = [Path(os.path.join(path, file)) for file in sheet_files]

        expected_files = set().union(image_files, item_files, sheet_files)

        real_files = set()

        glob_generator = list(Path(path).glob("**/*"))

        for posix_path in glob_generator:
            if os.path.isfile(posix_path._str): # Breakpoint
                real_files.add(posix_path)


        lack = expected_files.difference(real_files)
        extra = real_files.difference(expected_files)

        assert bool(lack) == False, "Lack of files: {}".format(lack)
        assert bool(extra) == False, "Extra files: {}".format(extra)
/home/michael/PycharmProjects/venv/photoarchive_4/bin/python /home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/pydevd.py --multiproc --qt-support --client 127.0.0.1 --port 43849 --file /home/michael/PycharmProjects/photoarchive_4/manage.py checkfiles
warning: Debugger speedups using cython not found. Run '"/home/michael/PycharmProjects/venv/photoarchive_4/bin/python" "/home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/setup_cython.py" build_ext --inplace' to build.
pydev debugger: process 3840 is connecting

Connected to pydev debugger (build 171.4694.67)
Traceback (most recent call last):
  File "/home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/pydevd.py", line 1591, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/pydevd.py", line 1018, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/michael/PycharmProjects/photoarchive_4/manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/michael/PycharmProjects/photoarchive_4/general/management/commands/checkfiles.py", line 59, in handle
    self._reveal_lack_extra_files()
  File "/home/michael/PycharmProjects/photoarchive_4/general/management/commands/checkfiles.py", line 39, in _reveal_lack_extra_files
    if os.path.isfile(posix_path._str):
AttributeError: _str

Process finished with exit code 1
/home/michael/pycharm项目/venv/photoarchive_4/bin/python/home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/pydev.py--multiproc--qt支持--client 127.0.0.1--port 43849--file/home/michael/pycharm项目/photoarchive_4/manage.py checkfiles
警告:找不到使用cython的调试器加速。运行'/home/michael/PycharmProjects/venv/photoarchive_4/bin/python”“/home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/setup_cython.py“build\u ext--inplace”进行构建。
pydev调试器:进程3840正在连接
已连接到pydev调试器(构建171.4694.67)
回溯(最近一次呼叫最后一次):
文件“/home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/pydevd.py”,第1591行,在
globals=debugger.run(setup['file'],None,None,is_模块)
文件“/home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/pydevd.py”,第1018行,运行中
pydev_imports.execfile(文件、全局、局部)#执行脚本
文件“/home/michael/Documents/pycharm-community-2017.1.5/helpers/pydev/_-pydev_-imps/_-pydev_-execfile.py”,execfile中第18行
exec(编译(内容+“\n”,文件,'exec'),全局,loc)
文件“/home/michael/PycharmProjects/photoarchive_4/manage.py”,第22行,在
从命令行(sys.argv)执行命令
文件“/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site packages/django/core/management/_init__.py”,第364行,从命令行执行
utility.execute()
文件“/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site packages/django/core/management/_init__.py”,第356行,执行
self.fetch_命令(子命令)。从_argv(self.argv)运行_
文件“/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site packages/django/core/management/base.py”,第283行,从_argv运行
self.execute(*args,**cmd_选项)
文件“/home/michael/PycharmProjects/venv/photoarchive_4/lib/python3.6/site packages/django/core/management/base.py”,第330行,执行
输出=self.handle(*args,**选项)
文件“/home/michael/PycharmProjects/photoarchive_4/general/management/commands/checkfiles.py”,第59行,在handle中
self.\u暴露\u缺少\u额外的\u文件()
文件“/home/michael/PycharmProjects/photoarchive\u 4/general/management/commands/checkfiles.py”,第39行,在“显示”和“缺少”额外文件中
如果os.path.isfile(posix_path.\u str):
属性错误:_str
进程已完成,退出代码为1

您在路径上使用的是
\u str
属性,该属性未记录且不保证设置。通常,下划线前缀表示这是用户代码不应使用的私有属性。如果要将路径转换为字符串,只需使用
str(路径)


但是在这种情况下,您不需要这样做:
Path
对象有一个方法,您可以调用它。另一种可能是将
Path
对象本身传递给函数,该函数在Python 3.6上受支持。

Path
来自何处?Patrick,它来自pathlib导入路径。我现在把它添加到了问题中。看起来您正在运行Python 3.6,因此
isfile
应该接受类似路径的对象。如果os.path.isfile(posix_路径)非常感谢,请尝试
。完美的解释。