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

kom*_*waj 2 python

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

D:\Komal\MyPrograms\Pkg\stemwordwww.yahoo.com42015-03-18 16-31
Run Code Online (Sandbox Code Playgroud)

但我想存储名为

www.yahoo.com42015-03-18 16-31 
Run Code Online (Sandbox Code Playgroud)

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

D:\Komal\MyPrograms\Pkg\stemword\www.yahoo.com42015-03-18 16-31
Run Code Online (Sandbox Code Playgroud)

代码

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
Run Code Online (Sandbox Code Playgroud)

phy*_*ion 5

你在这一行有一个错误:

dirPath2 = os.path.join(dir1+filename)
Run Code Online (Sandbox Code Playgroud)

它应该是:

dirPath2 = os.path.join(dir1,filename)
Run Code Online (Sandbox Code Playgroud)