Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1075343
  • 博文数量: 120
  • 博客积分: 887
  • 博客等级: 准尉
  • 技术积分: 1883
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-05 21:10
个人简介

RHCE,CCNA,熟悉shell脚本

文章分类

全部博文(120)

文章存档

2015年(16)

2014年(20)

2013年(48)

2012年(20)

2011年(16)

分类: Python/Ruby

2013-11-19 16:26:37

'''
a        ---如果没有文件存在,可以自动创建文件
r+        ----如果没有文件存在,程序直接报没有文件错误
w+    -----如果没有文件,可以自动创建文件,也能通过write()将字符串写入,但无法传入第二个文件
a+        ---如果没有文件,可以自动创建文件,也能用read()读取文件内容,但write()到第二个文件时,格式变为原始格式
'''

#! /usr/bin/python
import os
print "we will create a new file\n"
filename1 = raw_input('pls input your file1 name: \n')
createfile = open(filename1,'a+')
#createfile.truncate()
line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")
createfile.writelines('%s\n%s\n%s\n' % (line1,line2,line3))
print "%s input done!" % filename1

createfile.flush()                                    ---如果没有此步,第二个文件无法获得第一个文件中的内容,flush()即为将内存写入磁盘,个人这么理解

if os.path.exists(filename1) == True:
        print "we will transfer strings in filename2."
        filename2 = raw_input('input file2 name: \n')
        createfile2 = open(filename2,'a+')
        readfile = open(filename1)                    --若没有此步,第一个文件中input的数据,无法被再次read(),会一直无法将内容传递给第二个文件
        strfile = readfile.read()
        print strfile                                            ---用它来判断下是否有内容传递
        createfile2.write(strfile)

print "all done!"

createfile2.close()
createfile.close()
阅读(7947) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~