偷懒的小方法

新手...还在用IDLE.

每次想要测试多行的代码时,都需要新建一个txt文本,然后改后缀,~~~~

然后就写了个py用于创建空白的py

import datetime
wjlx='.py'
s=datetime.datetime.now()
s=str(s)+wjlx
s=s.replace(' ','-')
s=s.replace(':','-')
with open(s,"w",encoding='utf-8') as f:
    pass

这么简单的事情还栽了两个坑... ̄へ ̄

1,str对象的replace()方法会生成新的字符串,而不是改动原字符串

2,文件命名不允许出现冒号,空格

然后,我的笔记文件夹也需要这样的py

wjlx = '.bj'
s = '默认的名字'+wjlx
with open(s,"w",encoding='utf-8') as f:
    pass

 

posted @ 2017-11-30 20:08  ansver  阅读(169)  评论(0编辑  收藏  举报