實例如下所示:
#coding=utf-8
'''
Created on 2012-5-29
@author: xiaochou
'''
import os
import time
def nsfile(s):
'''The number of new expected documents'''
#判斷文件夾是否存在,如果不存在則創建
b = os.path.exists("E:\\testFile\\")
if b:
print("File Exist!")
else:
os.mkdir("E:\\testFile\\")
#生成文件
for i in range(1,s+1):
localTime = time.strftime("%Y%m%d%H%M%S",time.localtime())
#print localtime
filename = "E:\\testFile\\"+localTime+".java"
#a:以追加模式打開(必要時可以創建)append;b:表示二進制
f = open(filename,'ab')
testnote = 'private String username'
f.write(testnote.encode('utf-8'))
f.close()
#輸出第幾個文件和對應的文件名稱
print("file"+" "+str(i)+":"+str(localTime)+".txt")
time.sleep(1)
print("ALL Down")
time.sleep(1)
if __name__ == '__main__':
s = int(input("請輸入需要生成的文件數:"))
nsfile(s)
本想利用Python 讀取excel 文件,對應生成domain 文件,但是由于時間關系,暫時先不開發了,這里先生成.java 文件,未來有時間進行開發,也算給編程,代碼搬運工減少工作量。
以上這篇Python3.5 創建文件的簡單實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持我們。
本文標題: Python3.5 創建文件的簡單實例
本文地址: http://www.cppcns.com/jiaoben/python/226378.html