Home  >  Article  >  Backend Development  >  Simple example of creating a file in Python3.5

Simple example of creating a file in Python3.5

不言
不言Original
2018-04-27 10:08:321705browse

下面为大家分享一篇Python3.5 创建文件的简单实例,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧

实例如下所示:

#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 文件,未来有时间进行开发,也算给编程,代码搬运工减少工作量。


The above is the detailed content of Simple example of creating a file in Python3.5. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn