【问题标题】:VScode - print not workingVScode - 打印不工作
【发布时间】:2018-01-16 15:50:07
【问题描述】:

尝试做几个简单的打印语句,我认为 VScode 正在咳出一个毛球,或者我错过了一些东西。我使用的是 Windows 10。

示例1:(此文件夹中有10张.jpg图片)

import os
from PIL import Image

for f in os.listdir('.'):
    if f.endswith('.jpg'):
    print(f)

输出:

[Running] python "C:\img\resize.py"

[Done] exited with code=0 in 0.421 seconds

我的理解是,这将打印出与.py 文件保存在同一目录中的所有.jpg 文件。

更新:准确的代码格式

import os
from os import listdir

for f in os.listdir('.'):
    if f.endswith('.jpg'):
        print(f)

输出:

[Running] python "c:\img\tempCodeRunnerFile.py"

[Done] exited with code=0 in 0.24 seconds

我是否仍然正确地假设如果工作目录中有一个 .jpg 文件,也就是 resize.py 文件所在的目录,它会将它们全部列出来?

Directory output:
PS C:\img> ls
    Directory: C:\img
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        1/16/2018  10:14 AM            110 resize.py
-a----       11/11/2017   5:01 PM         177632 Scenery_003.jpg
-a----       11/11/2017   5:01 PM         256268 Scenery_004.jpg
-a----       11/11/2017   5:02 PM         153166 Scenery_005.jpg
-a----        1/16/2018  10:01 AM             88 test.py

我想我明白了,不是我想要的,但是......

import os
from os import listdir

for f in os.listdir("c:\\img\\"):
    if f.endswith('.jpg'):
        print(f)

输出:

[Running] python "c:\img\tempCodeRunnerFile.py"
Scenery_003.jpg
Scenery_004.jpg
Scenery_005.jpg

[Done] exited with code=0 in 0.234 seconds

【问题讨论】:

    标签: python visual-studio-code


    【解决方案1】:

    你的格式错误:

        for f in os.listdir('.'):
        if f.endswith('.jpg'):
            print(f)
    

    print(f) 必须右移 除此之外,代码可以在我的机器上运行。

    你试过 VS-Code 之外的代码吗?

    【讨论】:

    • 必须是 VScode 的东西,然后导致它在我的末端右移,我仍然遇到同样的问题,因为输出完成并没有文件迭代。
    • 你试过VS-Code之外的代码吗?在你的终端里?
    • 您看到正在使用代码运行器扩展。您可能想尝试使用 python vscode 扩展 (marketplace.visualstudio.com/items?itemName=ms-python.python),您可以在不调试的情况下运行代码。 (Ctrl+f5)。还有其他方法,包括“在终端中运行 Python 运行 Python 文件”(但在某些情况下,您可能会很乐意配置工作目录)
    【解决方案2】:

    你的假设有些偏差:My understanding is that this would print out all jpg files in the same directory that the .py file is saved.

    .-文件夹指的是当前工作目录。这可能是也可能不是目录,文件在其中。

    还有@jfk提到的格式问题

    【讨论】:

    • 感谢您的回复,我在上面更新了更多信息。目录 id 中文件的确切格式和列表期望它打印出来。还有其他想法吗?
    猜你喜欢
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 2019-10-21
    • 2023-01-29
    • 2015-11-03
    • 1970-01-01
    相关资源
    最近更新 更多