newfile.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:stlab 作者: yausern 项目源码 文件源码
def newfile(prefix,idstring,colnames=None,mypath = './',usedate=True,usefolder=True):

    import __main__
    if hasattr(__main__, '__file__'):
        mainfile = __main__.__file__
    else:
        mainfile = None
    print(mainfile)
    mytime = datetime.datetime.now()
    datecode = '%s' % mytime.year + '_' + ('%s' % mytime.month).zfill(2) + '_' +('%s' % mytime.day).zfill(2)
    timecode = ('%s' % mytime.hour).zfill(2) +'.' + ('%s' % mytime.minute).zfill(2) +'.' + ('%s' % mytime.second).zfill(2)

    if usedate:
        foldername = prefix + '_' + datecode+'_'+timecode+'_'+idstring
    else:
        foldername = prefix + '_' +idstring
    filename = foldername+'.dat'
    if usefolder:
        fullfoldername = os.path.normpath(mypath + '/' + foldername)
        fullfilename = os.path.normpath(mypath + '/' + foldername + '/' + filename)
    else:
        fullfoldername = os.path.normpath(mypath + '/')
        fullfilename = os.path.normpath(mypath + '/' + filename)
    print(fullfoldername)
    print(fullfilename)
    if not os.path.exists(fullfoldername):
        os.makedirs(fullfoldername)
    print('Measurement Name: ', foldername)
    if mainfile !=None and usefolder:
        scriptname = os.path.basename(mainfile)
        shutil.copyfile(mainfile,os.path.normpath(fullfoldername + '/' + scriptname))
    myfile = open(fullfilename, 'w')
    if colnames!=None:
        varline = '#' + ', '.join(colnames) + '\n'
        myfile.write(varline)
    return myfile
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号