
创始人Lv3
指定路径,文件名,判断并自动创建
# -*- coding: UTF-8 -*- import os, sys city = 'cd' path = "z:\\down\\test11\\" + city txtpath = 'filename' isExists=os.path.exists(path) if not os.path.exists(path): print '目录不存在' os.makedirs(path.decode('utf-8')) print '文件夹:' + path print "当前进程工作目录 : %s" % os.getcwd() else: print '目录已经存在....' print '文件夹:' + path print "当前进程工作目录 : %s" % os.getcwd() with open(path + "\\{}.txt".format(txtpath), "a") as f: f.write('456') f.close()
目录已经存在.... 文件夹:z:\down\test11\cd 当前进程工作目录 : C:\Users\Administrator
为filename.txt 写入'456'
0 已被阅读了68次 楼主 2018-10-18 11:56:40