笔记二 :print 以及基本文件操作

笔记一已取消置顶链接地址 http://www.cnblogs.com/dzzy/p/5140899.html

暑假只是快速过了一遍python ,现在起开始仔细学一遍

基础操作:

#python newer
#-*- coding:utf-8 -*-
print "我能吞下玻璃而不伤身体" #a shit print 3+1>2*9
print 2333333*2.0/2
shit=2333333.333
print shit name='qt shit'
mane='hahahaha'
print name
print "我真能吞下玻璃%r %d" %(name,shit)#行中变量,注意%r
print "我真能吞下玻璃%s %d" %(name,shit)#行中变量,注意%s
print name+' '+mane#字符串加 x="You shit , do you know? %d ....o(^▽^)o" %2333333
print x print "." *10 #连续输出 shit= "%r %r %r %r"
print shit %(1,2,3,4)
print shit %("qt"," ","shit","")
print shit %("Do you know? ",
"Qt shit! ",
"hahahaha",
"") months="Ja\nFe\nMa\nAp\nMa\n....."#\n回车 \t Tab键
print months
print """
There are 12 months a year,
Don't you know?
shit , 2333333
""" #段输出 shit="i'm\\a\\cat" #两个只输出一个
print shit
shit="hahaha \"xxoo\" do you know?"#""的嵌套家转义符\
print shit
shit="\a\f" #响铃,进纸
print shit print "现在开始测试输入了!"
print "How old are you?"
#age=raw_input()#获得输入
print "Do you love me ,ff?"
#yes=raw_input()
#print "%s ,I love %s year old you ,ff"%(yes,age) #shit=raw_input("Your name? ")#输入直接带提示字
#print shit from sys import argv
script=argv
#script,first,second,third=argv #注释掉的行实现获得
print "script:",script
#print "first:",first #运行时获取依次在终端输入的命令
#print "second:",second #运行时输入命令的参数不够会报错
#print "third:",third #想在程序运行前就输入变量值,那么就用这种方法 lookme=">>" #相当于宏定义
likes=raw_input(lookme)
print likes

文件操作:

#-*- coding:utf-8 -*-
from sys import argv #argv读一个文件名
script,filename = argv
"""
txt=open(filename)
print "Here is what I want to say:%r" % filename
print txt.read()
txt.close() #打开要关 print "Input a txt name:"
filee=raw_input(">")
filee=open(filee)
print filee.read()
filee.close()
""" print "下面是文档清空:\n%r文档将被重写:" %filename
raw_input("Are you sure?")
target=open(filename,'w') #'w'写入'r'读取'a'追加
target.truncate(0) #文件清空
print "Done"
print "输入内容3行:"
line1=raw_input("line1")
target.write(line1)
target.write("\n")
line2=raw_input("line2")
target.write(line2)
target.write("\n")
line3=raw_input("line3")
target.write(line3)
target.write("\n")
print "Closing"
target.close()

文件copy到另一个文件:(注意输入参数实例: python 1.py 2.txt 1.txt)

#-*- coding:utf-8 -*-
from sys import argv
from os.path import exists #import 调用exists判断文件是否存在
print"我要将一个文件复制到另一个文件"
script,fromm,too=argv
print"copy %s to %s " %(fromm,too)
ins=open(fromm) #先打开在读
indata=ins.read() print"%s has %d bytes long!" %(fromm,len(indata))#len print"%s exists? %s" %(too,exists(too))#exists
raw_input("sure?")
outs=open(too,'w')
outs.write(indata) print"Done!"
outs.close()
ins.close()