Python:尝试打印字符串时出现问题

Sri*_*har 0 python printing

import os
from os import path
import datetime
from datetime import date, time, timedelta
import time


def main():
    # Print the name of the OS
    print(os.name)

    # Check for item existence and type
    print("Item exists: " + (str(path.exists("textfile.txt"))))
    #print("Item is a file: " + str(path.isfile("textfile.txt")))
    #print("Item is a directory: " + str(path.isdir("textfile.txt")))

if __name__ == "__main__":
    main()
Run Code Online (Sandbox Code Playgroud)

错误:

print("Item exists:"+(str(path.exists("textfile.txt"))))
TypeError:'str'对象不可调用

Isr*_*man 5

根据你得到的错误,你的代码中的某个地方(你的帖子中没有出现)你分配给str...

阅读评论后添加:

这段代码运行正常.也许你从像spyder这样的IDE运行它,它会记住你之前在shell中或在执行的代码中分配的变量.请尝试从Windows"DOS"shell运行它,看到它再次出现错误.如果没有发生,你可以重新启动IDE并发现它也已经消失了