|
#!/usr/bin/env python
import os #导入功能模块
url ="/tmp/" #变量定义
def check(): #函数定义
while True: #一直循环 提示用户输入正确的要创建的文件名
os.system("cd /tmp")
d_com=raw_input("Please input filename") #将用户输入的值赋给变量做判断
if os.path.exists(url+d_com) == True: #调用功能模块判断文件是否存在
print "error!! file had existed" #输出判断结果
continue #存在时重新输入文件名
else:
while True: #当要创建的文件不存在
M = open(url+d_com,'a') # 提示用户向创建的文件输入内容
A = raw_input("piease input what's you want add")
M.write(A) #将内容写入创建的文件
M.flush
M.close()
if A == "":
exit() #当输入为空时代表输入结束,退出函数。
check()
========================================================================================
#!/usr/bin/env python
#coding:utf-8
import os
list = []
def check():
while True:
d_com=raw_input("Please input filename")
if not os.path.exists(d_com):
break
print "error!! file had existed"
while True:
test = raw_input("请输入内容,.结束")
list.append(test + "\n")
if test == ".":
break
data = open(d_com,'w')
data.writelines(list)
data.close()
check() |
|
|