一 脚本文件+argv
(1) 简单的说就是一段自己写的,可运行的代码,否则会报错
(2)简单脚本,直接 python xxx1.py
(3)带参数脚本,需要 python xxx2.py argv1 argv2 argv3
根据脚本参数的数量,传入对应的参数,空格分隔即可,否则会报错,无法解包unpack
(4)脚本的参数argv,在调用脚本时,就得传入,否则报错
而如果使用 raw_input 就会在脚本运行过程中,运行到那里才会需要用户输入。
# -*- coding:utf-8 -*-
from sys import argv
script,first,second,third=argv
print "the script is called:" ,script
print "the first var is :" ,first
print "the second var is :" ,second
print "the third var is :" ,third
二 脚本文件和读和写,open(),read(),close()
试验了脚本的读取和写入,发现自己出了不少问题。。。
挨个改正吧,还有一些问题待查
# -*- coding:utf-8 -*-
from sys import argv
script,target_filename=argv #target_filename =是要操作的文件
prompt=">>please enter here:>>"
aaa=open(target_filename)
print("the file content now is:")
print open(target_filename).read()
print "1"
print aaa.read(),aaa.read(),aaa.read()
print "2"
print "3",aaa.read()
print "4"
print "5\n",open(target_filename).read()
print("will you want to clear and rewrute the file: %s ?") %target_filename
print("you can enter \"ok\" to coutinue,or any other else")
user_option=raw_input(prompt)
print("the file content now is:")
print aaa.read(),aaa.read(),aaa.read()
print open(target_filename).read()
target=open(target_filename,"w") #要操作的内容,一旦开启写的模式,就无法读内容了?!
print "the file content now is:",open(target_filename).read()
if user_option=="ok":
print("ok,let us start,truncate this file,please wait...\n......\n...")
target.truncate()
print("ok,file %s is truncated") % target_filename
print("the file content now is:")
print open(target_filename).read()
else:
quit()
print("now,let us add something to this file %s") %target_filename
print("please enter line1:")
add_line1=raw_input(prompt)
print("please enter line2:")
add_line2=raw_input(prompt)
print("please enter line3:")
add_line3=raw_input(prompt)
print("ok,wo have three new lines ,let us write the file")
target.write(add_line1)
target.write("\n")
target.write(add_line2)
target.write("\n")
target.write(add_line3)
target.write("\n")
print("let us do it ,please wait......\n.......\n...")
print("ok,this file is rewrite,thats great!!")
print("the file content now is1:")
print open(target_filename).read()
target.close()
print("the file content now is2:")
print open(target_filename).read()
"""
犯错累计,我犯得这些错好低级好典型...
低级错误
1 truncate 拼错为turncate
2 脚本的参数名字,完整文件名写错,路径,后缀名各种不对
3 语法不熟,raw_input() 而不是rawinput()
4 语法不熟,if : else: 写成 if : elseif:就报错
5 block不对,把if那一块,弄了下,重新输入tab就好了
6 输入了中文字符
逻辑概念错误
1 弄混了 target_filename 和target,实际操作的是 target = open(target_filename)
并且为了后面修改 target = open(target_filename,"w")
需要先打开文件,并且操作的是文件内容。
后面关闭的,读取的都是 open()内容 #只有open了才读到了程序里来,才可调用,之前只是原始文件。
open("file.txt").read
open("file.txt").close
2 试图用print敲入分行,但没理解print是屏幕输出,不会修改到文件里的
target.write("\n")
print ("\n")
语法错误
1 语法不熟 target.write=add_line1 没这种写法
target.write(add_line1)
记下,这里会可以 target.write(line1,"\n")---???一会试试
不行?好像只能接受1个参数
2 显示文件内容
#print (target) #这样是打印不出来的,要打印的是read()的内容才可以
print open(target_filename).read() #只有这样可以?这样可以保证每次都先打开再读出来
print (target.read) #此前必须有一句 open(target_filename),而且不能是W模式
#否则会提示file ont open for reading
#这样只能读1次,后面继续读为空
target=open(target_filename,"w") #要操作的内容,一旦开启写的模式,就无法读内容了?!
print("the file content now is:",open(target_filename).read())
试验是这样,必须在W模式下,写完后,close()文件后才可以重新读出内容来。
3 close语法不会用,记得是需要关闭的,但不会---???
#close(target_filename)
target.close()
open(target_filename).close
4 记得可以用不区分大小写的---???
现在不会,只简单些了 if "OK" or "ok"
5 如果做到限制用户输入,是要求的格式?要求的字数以上? ---???
6 如何做到兼容识别多种输入?---???
7 quit()不对?
跳出的地方不确定
quit()
.quit()
.exit()
8 if user_option=="ok" or "OK":
"""
四 file相关的几个方法
(1) 首先要读入 open(filename)
open(filename)
open(filename,'r')
open(filename,'w')
open(filename,'r+')
open(filename,"w+")
open(filename,"a+") #append 追加
(2) 然后处理读入的文件
open(filename).read()
open(filename).readline() 读取文本的一行,第1行?数字表示第1行的字符个数?
open(filename).truncate()
open(filename).write()
open(filename).close()
五 尝试写的用脚本复制其他文件的不同情况
# -*- coding:utf-8 -*-
#试验文件之间的copy
from sys import argv
from os.path import exists
script,from_file,to_file=argv
user_option=raw_input("we will copy file %s to file %s,ok?>>" %(from_file,to_file))
if user_option=="ok":
print"ok,let us start..."
print("does the outfile exists? %r") % exists(to_file)
if exists(to_file):
print("the outfile exists,let us do the work")
print("please wait ... ...\n...\n...")
#text=open(from_file).read() #这里可以连着写
in_file=open(from_file)
text=in_file.read()
out_file=open(to_file,"w") #这里两句不能连着写,否则就报错,现在也不明白?
out_file.write(text)
print("thats all,OK,this is the file %s's new content") %to_file
out_file.close() #这里不先关闭就无法打开读内容,现在还不明白原因?
print open(to_file).read()
else:
print("the outfile does not existed,quit")
quit()
else:
quit()
out_file.close()
in_file.close()
# -*- coding:utf-8 -*-
#试验文件之间的copy
from sys import argv
from os.path import exists
script,from_file,to_file=argv
user_option=raw_input("we will copy file %s to file %s,ok?>>" %(from_file,to_file))
if user_option=="ok":
print"ok,let us start..."
print("does the outfile exists? %r") % exists(to_file)
if exists(to_file):
print("the outfile exists,let us do the work")
print("please wait ... ...\n...\n...")
#text=open(from_file).read() #这里可以连着写
in_file=open(from_file)
text=in_file.read()
#out_file=open(to_file,"w")
#out_file.write(text)
open(to_file,"w").write(text) #这里两句也可以连着写
#print("thats all,OK,this is the file %s's new content") %to_file
#out_file.close() #这里不先关闭就无法打开读内容,现在还不明白原因?
#print open(to_file).read()
else:
print("the outfile does not existed,quit")
quit()
else:
quit()
#out_file.close()
#in_file.close()