gpt4 book ai didi

python - 将文件保存在当前目录的文件夹中

转载 作者:行者123 更新时间:2023-11-30 23:13:47 27 4
gpt4 key购买 nike

我想创建一个名为当前目录+文件夹+系统日期和时间的文件。我得到的输出为 -

D:\Komal\MyPrograms\Pkg\stemwordwww.yahoo.com42015-03-18 16-31

但我想存储名为

的文件
www.yahoo.com42015-03-18 16-31 

在文件夹stemword中,即要求输出为

D:\Komal\MyPrograms\Pkg\stemword\www.yahoo.com42015-03-18 16-31

代码

def create_file(self,filename,folder):
print 'creating file....'
print 'file is---'
print filename
#Here our filename is url eg-www.amazon.in
dir = os.getcwd()
dir1 = os.path.join(dir,folder)
print 'directory---'
print dir1
date = datetime.datetime.now()
now = date.strftime("%Y-%m-%d %H-%M")
dirPath2 = os.path.join(dir1+filename)
dirPath = dirPath2.rstrip('\n')
filenameCreated = dirPath+now
print 'file is ---'
print filenameCreated
f = self.openfile(filenameCreated + '.txt', 'a')
f.close()

return filenameCreated

最佳答案

此行有错误:

dirPath2 = os.path.join(dir1+filename)

应该是:

dirPath2 = os.path.join(dir1,filename)

关于python - 将文件保存在当前目录的文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29120431/

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