python笨办法学习-practice20

函数和文件

回忆一下函数的要点,然后一边做这节练习,一边注意一下函数和文件是如何在一起协作发挥作用的。

新建一个ex.txt文本文档

To all the people out there.

I say I don't like my hair.

I need to shave it off.

from sys import argv #从sys里调用argv

script,input_file = argv #解包参数

def print_all(f):#定义print_all函数,f为函数的参数-文件名
    print(f.read()) #函数内容为读取

def rewind(f):
    f.seek(0)# 使用seek(),默认一开始在开头,移动0个位置

def print_a_line(line_count,f):
    print(line_count,f.readline())#readline()读取整行

current_file = open(input_file)#打开文件

print("First let's print the whole file:\n")

print_all(current_file)#使用print_all函数

print("Now let's rewind,kind of like a tepe.")

rewind(current_file)#使用rewind函数

print("Let's print three lines:")

current_line = 1
print_a_line(current_line,current_file)

current_line = current_line + 1 #current_line += 1(增量幅值)
print_a_line(current_line,current_file)

current_line = current_line + 1 #current_line += 1(增量幅值)
print_a_line(current_line,current_file)

使用cmd执行命令

文件地址:

"D:\Python Project\practice\ex13.py" "D:\Python Project\practice\ex.txt"

cmd运行结果

First let's print the whole file:

To all the people out there.

I say I don't like my hair.

I need to shave it off.

Now let's rewind,kind of like a tepe.

Let's print three lines:

1 To all the people out there.

2 I say I don't like my hair.

3 I need to shave it off.

Python-seek()函数:移动光标或指针

seek函数能够帮助我们File Handle,即文件处理

在Python中,我们可以将seek()最简单理解为:移动光标或指针

由于不能用鼠标移动光标,我们可以用seek()将光标移动到我们想要的位置,然后对文件进行写入等操作

参数

Syntax: f.seek(offset, whence),f指的是file(或$你的文件名)

Parameters:

offset: 将光标向前移动n个位置

whence: 参考位置,一般参数为0,1,2

0 :将开头作为参考位置

1 :将当前作为参考位置

2 :将末尾作为参考位置

(所谓参考位置(reference point)一开始你想在哪)

————————————————

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

                        

原文链接:https://blog.csdn.net/ONEmoreOrange/article/details/118252278

Python-readline()函数-读取整行

概述

readline() 方法用于从文件读取整行,包括 "\n" 字符。如果指定了一个非负数的参数,则返回指定大小的字节数,包括 "\n" 字符。

语法

readline() 方法语法如下:

fileObject.readline(size)

参数

size -- 从文件中读取的字节数。

返回值

返回从字符串中读取的字节。

python赋值语句----增量赋值(augmented assinment)

增量赋值包含一个二元表达式和一个赋值表达式。如下:

a += b

a = a + b

以上两种格式效果等同

被赋值的对象为列表(可变对象)时,两种形式的赋值略有区别:

“+=”会在原有位置修改对象,而“+”会创建一个新的对象:

其中 +、-、*、/、%都可以实现这种操作

增量赋值示例

def fu_zhi_zeng_liang(number):
    a = number
    a += number
    print("方法1:5+5=",a)

fu_zhi_zeng_liang(5)

def fu_zhi_zeng_liang2(number):
    a = 2
    a += number
    print("方法2:2+8=", a)

fu_zhi_zeng_liang2(8)

def fu_zhi_zeng_liang3(number):
    a = 3
    a += number
    print("方法3:3+7=%d" % int(a))

fu_zhi_zeng_liang3(7)

def fu_zhi_zeng_liang4(number):
    a = 10
    a *= number
    print("方法4:10*1=%d" % int(a))

fu_zhi_zeng_liang4(1)

def fu_zhi_zeng_liang5(number):
    a = 100
    a /= number
    print("方法5:100/1=%d" % int(a))

fu_zhi_zeng_liang5(10)

#字符串的拼接
b ="py"
b +="th"
b +="on"
print("字符串的拼接:",b)

#两种赋值方法对比
c = [11,22,33]
d = [1,2,3]
d += c
e = d + c
print("d+=c:",d)
print("e=d+c:",e)

  • 34
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值