本文摘自php中文网,作者不言,侵删。
下面为大家分享一篇Python3.5 创建文件的简单实例,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧实例如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #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 创建文件的简单实例的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python如何安装numpy库
Python semaphore(信号量)是什么?(实例详解)
Python 爬虫安装什么包
什么是Python type 函数?type 函数使用示例
Python实现发短信的方法介绍(附代码)
利用 Python 对目录下的文件进行过滤删除实例详解
Python如何将字符串常量转化为变量?(附示例)
Python爬虫能够干什么
Python如何对excel数据进行处理
Python中divmod函数的用法
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python3.5 创建文件的简单实例