gpt4 book ai didi

python - 类型错误 : 'str' object is not callable (create a folder in the same directory)

转载 作者:太空宇宙 更新时间:2023-11-04 08:09:53 24 4
gpt4 key购买 nike

我想在同一个目录下创建一个文件夹,哪里出错了?

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

最佳答案

os.curdir不是函数而是常量字符串。只是作为一个值:

root = os.curdir

当您这样做时,您可以使用 os.path.join 改进您的代码.

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

关于python - 类型错误 : 'str' object is not callable (create a folder in the same directory),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24656591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com