python程序设计基础5:python文件使用(遍历一个文件夹中所有的图片)

本文介绍了Python文件操作的基础知识,包括创建、读取TXT文件,使用read()、readline()、readlines()函数,二进制文件的存取,以及复制文件的函数实现。同时,展示了如何遍历文件夹中的图片。
摘要由CSDN通过智能技术生成

文件的操作是非常常用的。下面通过基本的例子了解对一般的TXT文档的操作。

1.创建一个TXT文本,在其中写入学生的基本信息。包括:姓名,性别,地址4个信息。

代码:

11 #!/usr/bin/env python
 10 # encoding: utf-8
  9                                            
  8 f=open('filecreat.txt','w')
  7 f.write('name'+'\tsex'+'\tcellphone'+'\t\taddress'+'\n')
  6 flag=1
  5 while flag==1:
  4     name=raw_input('please input a name:')
  3     sex=raw_input('please input the sex:')
  2     cellphone=raw_input('please input the cellphone number:')
  1     address=raw_input('please input the address:')
12      s=name+'\t'+sex+'\t'+cellphone+'\t\t'+address+'\n'                                                  
  1     f.write(s)
  2     flag=input('will you go on?(1/0):')
 3 f.close()

结果:

please input a name:iker

please input the sex:man
please input the cellphone number:123456789
please input the address:XMU
will you go on?(1/0):1
please input a name:456123
please input the sex:UESTC
please input the cellphone number:1234567894
please input the address:xsf
will you go on?(1/0):0
ikerpeng@~/text/mlexcersise$ python excersice2.py 
name sex cellphone address


iker man 123456789 XMU


456123 UESTC1234567894xsf

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值