我的代码看起来像这样: #!/usr/bin/python -tt # -*- coding: utf-8 -*- import -6ren">
gpt4 book ai didi

python - "bound method": How to create a file name in sub directory with python

转载 作者:太空宇宙 更新时间:2023-11-03 17:28:23 24 4
gpt4 key购买 nike

我尝试在子目录中创建具有随机名称的文件,但出现此错误:

 URL/<bound method webSiteCompanies.nameGenerator of
<__main__.webSiteCompanies instance at 0x7fc30dbccd88>>

我的代码看起来像这样:

#!/usr/bin/python -tt
# -*- coding: utf-8 -*-

import os
import string
import random


class webSiteCompanies():

def __init__(self):
self.path = "URL"
self.stringSize = 42

def nameGenerator(self):
chars = string.ascii_uppercase + string.digits
return str(''.join(random.choice(chars) for _ in range(self.stringSize)))


def createFile(self):
realPath = self.path + "/" + str(self.nameGenerator)
try:
os.mkdir(self.path)
except Exception:
pass

print realPath
os.mknod(realPath+".txt")

# Main
if __name__=='__main__':
bob = webSiteCompanies()
bob.createFile()

我的文件已创建,但名称错误...

如何解决我的问题?

最佳答案

您需要调用函数 - self.nameGenerator() 。示例-

realPath = self.path + "/" + str(self.nameGenerator())

关于python - "bound method": How to create a file name in sub directory with python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32247186/

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