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

2024-04-24 09:02:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个名为current directory+folder+系统日期和时间的文件。 我得到的结果是-

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

Tags: 文件isoswwwfolderfilenamenowdirectory
2条回答

这一行有错误:

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

应该是:

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

试试这个

  dirPath2 = dir1+"\"+filename

相关问题 更多 >