TypeError: 'str'对象不可调用(在同一目录创建文件夹)

1 投票
1 回答
2281 浏览
提问于 2025-04-18 12:39

我想在同一个文件夹里创建一个新文件夹,我哪里做错了呢?

def selection():
    print "Content Profiling"
    print "~~~~~~~~~~~~~~~~~"
    print " 'G', 'PG13', 'NC16', 'M18', 'R21' "
    selectionInput = raw_input("Please select your content profile")
    if selectionInput == "G":
        root = os.curdir()
        path = root+"/G"
        os.makedirs( path, 0755 );   

输出结果:

Traceback (most recent call last):
  File "C:\Python27\guicmd.py", line 44, in <module>
    selection()
  File "C:\Python27\guicmd.py", line 38, in selection
    root = os.curdir()
TypeError: 'str' object is not callable

1 个回答

6

os.curdir 不是一个函数,而是一个常量字符串。可以把它当作一个值来使用:

root = os.curdir

顺便提一下,你可以通过使用 os.path.join 来改善你的代码。

path = os.path.join(os.curdir, "G")
os.makedirs(path, 0o755)

撰写回答