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

时间:2015-03-18 11:08:13

标签: python

我想创建一个名为current directory + folder + system date and time的文件。 我得到输出as-

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

2 个答案:

答案 0 :(得分:4)

此行中有错误:

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

应该是:

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

答案 1 :(得分:-1)

试试这个

  dirPath2 = dir1+"\"+filename
相关问题