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

python中for循环

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2019-11-6 17:00:05 | 显示全部楼层 |阅读模式
import os
1 e) ]' E2 v& Z( R9 O2 H# Ppath = r"F:\Python\task"
  [. `4 r  p5 x3 F- W% F- Gotherpath=r"F:\Python\other"
' k3 w( b0 h% @: g" J8 bfor filename in os.listdir(path):
7 l1 L% M* C, O% z    print(path,filename)$ M' n9 w+ j. J% D% H8 ?: |5 n
    fullname=os.path.join(path,filename)& [: `) x, v3 E: V: m1 Y
    if os.path.isfile(fullname):        , T* w' J  R, K: ^4 R
          othername=os.path.join(otherpath,filename)  # L: V1 U, V8 }, `0 a8 W( @: H; `: m
          otherfile=open(othername,'wb')
+ h! C1 q5 m, ~- @8 L( w          for line in open(fullname,'rb'):
8 A4 m- i( w7 N% z3 m: U2 X  S              for c in line:% O1 L/ H. ^$ f0 g. m7 P8 z
                  if not c.isdigit():otherfile.write(c)
. k4 `) `. s% _- a2 l          otherfile.close()

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2019-11-6 17:00:06 | 显示全部楼层
# coding:utf-8
' ]: g9 C' U+ b9 Fpath = r"C:\Users\Administrator\Desktop\CSDN博客草稿\文件的读\password.txt"
! S2 H' F! ^6 |( ]% }7 q' b#传入要读的文件路径
+ n; d2 s1 R+ {' v' v, |file = open(path,"r",encoding="utf-8",errors="ignore")
; V% Q2 W. `! m# v* e2 G"""" M( T: W1 ~* \! M1 l# m
open表示打开你要执行的文件用读的方式打开
2 Y& c1 J. q! B1 p第一个参数是上面的文件path路径,第二个是所要执行的操作,(r)代表读,/ r2 D3 I& {, S# _1 _1 F
#encoding="utf-8表示指定编码为“utf-8”,errors="ignore"表示读的时候遇到错误忽略
/ T6 w; b- \; P"""1 Y; b# \4 M1 W: }9 @; m0 s
while True:
  [% C$ {! m% `# B  mystr = file.readline()#表示一次读取一行2 E( B; y. a+ Q& |1 y
  if not mystr:
1 L1 |/ B/ }) D- u  #读到数据最后跳出,结束循环。数据的最后也就是读不到数据了,mystr为空的时候: P9 e% u" S6 F2 n$ g6 b
    break
& ?5 v2 P/ u& b  print(mystr,end="")#打印每次读到的内容
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-12 01:39 , Processed in 0.016193 second(s), 22 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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