實(shí)例如下所示:
#coding=utf-8'''Created on 2012-5-29@author: xiaochou'''import osimport timedef nsfile(s): '''The number of new expected documents''' #判斷文件夾是否存在,如果不存在則創(chuàng)建 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:以追加模式打開(必要時(shí)可以創(chuàng)建)append;b:表示二進(jìn)制 f = open(filename,'ab') testnote = 'private String username' f.write(testnote.encode('utf-8')) f.close() #輸出第幾個(gè)文件和對應(yīng)的文件名稱 print("file"+" "+str(i)+":"+str(localTime)+".txt") time.sleep(1) print("ALL Down") time.sleep(1)if __name__ == '__main__': s = int(input("請輸入需要生成的文件數(shù):")) nsfile(s)本想利用Python 讀取excel 文件,對應(yīng)生成domain 文件,但是由于時(shí)間關(guān)系,暫時(shí)先不開發(fā)了,這里先生成.java 文件,未來有時(shí)間進(jìn)行開發(fā),也算給編程,代碼搬運(yùn)工減少工作量。
以上這篇Python3.5 創(chuàng)建文件的簡單實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選