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('
'*30)
        f.close()

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

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

要点:

  • 所要读写的文件名可以是_变量_
原文地址:https://www.cnblogs.com/yaos/p/6986274.html