python学习笔记(一):文件操作

【注】在文件已经存在时,对文件进行输入;否则,创建文件并对文件进行写入操作!
#!/user/bin/env/python
'''Please input the filename'''
filename = raw_input("Please input the fileName:")
import os
if os.path.exists(filename):
    print("What the filename you have inputed have been exist,and you will append into the file %s\n" %filename)
    f = open(filename,'a')
else:
    print("That File not exists!And will create one automatic!")
    f = open(filename,'w')
i = 0
while i < 2:
    i+=1
    input_str = raw_input("Please input the string what you want to save into the file %s\t" %filename)
    f.write(str(input_str)+"\n")
f.close()

你可能感兴趣的:(python,String,File,input,import)