Python文件读写(一)

import time as t
from os import path

def createFile(dest):
    date = t.localtime(t.time())
    name = '%d_%d_%d.txt' %(date[1],date[2],(date[0] %100)) # 2017 % 100 = 17

    if not (path.isfile(dest+name)):
        f = open(dest+name,'w')
        f.write('\n'*30)
        f.close()

if __name__ == '__main__':
    destination = '<PATH>'
    createFile(destination)
    raw_input('done')

查看当前目录下所要创建的6_11_17.txt文件是否已经存在,如果不存在就创建,并写入30个换行('\n'*30

要点:

  • 所要读写的文件名可以是_变量_
posted @ 2017-06-11 20:38  2021年的顺遂平安君  阅读(59)  评论(0编辑  收藏  举报