文件操作
fobj = open('/tmp/mima')
data = fobj.read() #read 默认读取全部内容
print(data)
fobj.close()
fobj = open('/tmp/mima')
fobj.read(4) # 适合读取4字节,建议一次读1024的倍数
fobj.readline() # 适合文本文件,读一行
fobj.readlines() # 适合文本文件,把所有行读到列表中
fobj.close()
--------------
fobj = open('/tmp/mima')
for line in fobj:
print(line,end=' ')
fobj.close()
print(line.strip()) # 把末尾的'\n'删掉
函数基础
自定义函数的简单规则:
函数代码块以def关键字开头,后接函数标识符名称和圆括号()
所有传入的参数和自变量都必须放在圆括号内,可以在圆括号中定义参数。
函数的第一行语句可以选择性使用文档字符串,用于存放函数说明。
函数内容以冒号:开始,并且要缩进。
return [表达式]结束函数,选择性返回一个值给调用方。不带表达式的return相当于返回None。
return有两个作用:
用来返回函数的运行结果,或者调用另外一个函数。比如max()函数
函数结束的标志。只要运行了return,就强制结束了函数。return后面的程序都不会被执行。其实,函数不一定有返回值。什么都不返回的函数不包含return语句,或者包含return语句,但没有在return后面指定值。*
def weight(a):
weight = a / 1000
print(f'输入的重量为{a}g,转化为的重量为{weight}')
weight(20)
sys.argv的用法
import sys
list = ['111','222','333']
print(list)
print(list[0])
list1=sys.argv
print(list1)
print(list1[1])
print(list1[2])
-----------------------------------------------------------------
然后运行
C:\Users\Thinkpad>python E:\python全栈\pycharm代码\Function\sys.py 123 456
['111', '222', '333']
111
['E:\\python全栈\\pycharm代码\\Function\\sys.py', '123', '456']
123
456
C:\Users\Thinkpad>python E:\python全栈\pycharm代码\Function\sys.py 123 456 44 444
['111', '222', '333']
111
['E:\\python全栈\\pycharm代码\\Function\\sys.py', '123', '456', '44', '444']
123
456
这才知道sys.argv[0]接收的是文件名(如果运行文件和运行终端不在同一路径下会接收其的路径及文件名)
sys.argv[1] 接收的的在终端传入的第一个参数
sys.argv[2]接收的的在终端传入的第二个参数
- 文件内移动
tell() : 返回当前文件指针的位置
fobj.seek(10,0) # 第一个数字是偏移量,第二个是数字的相对位置,0表示文件开头,1表示当前位置,2表示文件的结尾
seek:移动文件指针到不同的位置
函数的传参
import os
import sys
import shutil
import time
localtime = time.strftime("%Y%m%d",time.localtime(()
def word(src_file,origin_line,update_line):
bak_file = src_file + '.bak' + localtime
tmp_file = src_file + '.temp'
shutil.copy2(src_file,bak_file)
with open(src_file,mode='r') as fr, open(tmp_file,mode='w') as fw:
for line in fr:
if origin_line in line:
print "source_line:",line
fw.write(line.replace(origin_line,update_line))
else:
fw.write(line)
os.remove(src_file)
os.rename(tmp_file,src_file)
print "replace success"
return
def line_word(src_file,line_key,origin_line,update_line):
bak_file = src_file + '.bak' + localtime
tmp_file = src_file + '.temp'
shutil.copy2(src_file,bak_file)
with open(src_file,mode='r') as fr, open(tmp_file,mode='w') as fw:
for line in fr:
if line_key in line:
print "source_line:",line
fw.write(line.replace(origin_line,update_line))
else:
fw,write(line)
os.remove(src_file)
os.rename(tmp_file,src_file)
print("replace success")
return
def line_word(src_file,line_key,line_key1,origin_line,update_line):
bak_file = src_file + '.bak' + localtime
tmp_file = src_file + '.temp'
shutil.copy2(src_file,bak_file)
with open(src_file,mode='r') as fr, open(tmp_file,mode='w') as fw:
for line in fr:
if line_key in line:
print "source_line:",line
fw.write(line.replace(origin_line,update_line))
else:
fw,write(line)
os.remove(src_file)
os.rename(tmp_file,src_file)
print("replace success")
return
if __name__ == "__main__":
if len(sys.argv) == 4:
src_file,origin_line,update_line = sys.argv[1:]
word(src_file,origin_line,update_line)
elif len(sys.argv) == 5:
src_file , line_key, origin_line , update_line = sys.argv[1:]
line_word(src_file,line_key,origin_line,update_line)
elif len(sys.argv) == 6:
src_file , line_key , line_key1 , origin_line , update_line = sys.argv[1:]
line_word(src_file,line_key,line_key1,origin_line,update_line)
else:
print("传参错误!")
-----------------------------------------
import os
import sys
import shutil
import time
localtime = time.strftime("%Y%m%d", time.localtime())
def word(src_file,origin_line,update_line):
bak_file = src_file + '.bak' + localtime
tmp_file = src_file + '.temp'
shutil.copy2(src_file, bak_file)
with open(src_file, mode='r') as fr, open(tmp_file, mode='w') as fw:
for line in fr:
if origin_line in line:
print "source_line:", line
fw.write(line.replace(origin_line, update_line))
else:
fw.write(line)
os.remove(src_file)
os.rename(tmp_file, src_file)
print "replace success!"
return
def line_word(src_file,line_key,origin_line,update_line):
bak_file = src_file + '.bak' + localtime
tmp_file = src_file + '.temp'
shutil.copy2(src_file, bak_file)
with open(src_file, mode='r') as fr, open(tmp_file, mode='w') as fw:
for line in fr:
if line_key in line:
print "source_line:",line
fw.write(line.replace(origin_line, update_line))
else:
fw.write(line)
os.remove(src_file)
os.rename(tmp_file, src_file)
print "replace success!"
return
if __name__ == "__main__":
if len(sys.argv) == 4:
src_file, origin_line, update_line = sys.argv[1:]
word(src_file, origin_line, update_line)
elif len(sys.argv) == 5:
src_file, line_key, origin_line, update_line = sys.argv[1:]
line_word(src_file, line_key, origin_line, update_line)
else:
print("传参错误!!!")