找回密码
 注册
查看: 3688|回复: 1

python中for循环

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2019-11-6 17:00:05 | 显示全部楼层 |阅读模式
import os! i; O# s+ j! p+ K& J" |7 H+ F8 R/ b) h
path = r"F:\Python\task"8 @7 J# \6 E/ H& f  t
otherpath=r"F:\Python\other"
& `  q, K3 |- o$ _( V4 N; o+ V- Y" Ufor filename in os.listdir(path):
; t" v1 I' K- a* M% u    print(path,filename); j) T# n% I# w, s4 ?
    fullname=os.path.join(path,filename)( {$ J- `. ?# g$ d
    if os.path.isfile(fullname):        
- h* A* Q( Y0 i7 }9 h( e# u          othername=os.path.join(otherpath,filename)  " q) E% V( K7 L3 M  @
          otherfile=open(othername,'wb')
+ R% D; n& K5 Y) ~: Z$ x9 G- S2 I          for line in open(fullname,'rb'):3 N/ L2 ]1 @- T; }* B/ |, }: R
              for c in line:
4 n. J- F( A. p- m                  if not c.isdigit():otherfile.write(c)# r  G$ P9 b) G- ^8 A" N5 U. X
          otherfile.close()

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2019-11-6 17:00:06 | 显示全部楼层
# coding:utf-8( q5 h/ g$ l$ e
path = r"C:\Users\Administrator\Desktop\CSDN博客草稿\文件的读\password.txt"
, n! t  H) p, G3 G$ A#传入要读的文件路径: e% y2 J/ t0 b7 g" N  k
file = open(path,"r",encoding="utf-8",errors="ignore")
: r3 `4 f' V8 y"""
' q" Y3 D! `; Q/ dopen表示打开你要执行的文件用读的方式打开% J/ x9 j9 c* W. O
第一个参数是上面的文件path路径,第二个是所要执行的操作,(r)代表读,
- r2 j8 O$ ]8 H3 I6 C* z$ T#encoding="utf-8表示指定编码为“utf-8”,errors="ignore"表示读的时候遇到错误忽略; f" o( Q1 e! m+ Q" L7 N
"""
6 ^$ |; ]5 ^0 M' xwhile True:) z# x9 {- M" O; X8 ?
  mystr = file.readline()#表示一次读取一行
& R$ R& N1 N3 |2 j" T6 w  if not mystr:" F  j7 h6 \7 u7 c: ~5 r% S. f
  #读到数据最后跳出,结束循环。数据的最后也就是读不到数据了,mystr为空的时候( w" j$ y# c/ i! g
    break
: N) V$ ~' G, I# \% Z  print(mystr,end="")#打印每次读到的内容
您需要登录后才可以回帖 登录 | 注册

本版积分规则

返回首页|Archiver|手机版|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )

GMT+8, 2026-6-12 00:32 , Processed in 0.015018 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表