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

python中for循环

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2019-11-6 17:00:05 | 显示全部楼层 |阅读模式
import os, n2 O/ Z, e. B" l5 W8 I4 K9 l
path = r"F:\Python\task"
" t1 G0 b8 ]5 A/ {! G( |/ Lotherpath=r"F:\Python\other"
, Q8 ?7 x3 j' k# U( V" wfor filename in os.listdir(path):1 R* \7 P! ^3 d8 a8 m$ q5 p* A
    print(path,filename)! Z) @5 _0 x) j3 |( h
    fullname=os.path.join(path,filename)
* a/ {; u0 T- ?. U5 l    if os.path.isfile(fullname):        8 B. ]: J8 \$ H
          othername=os.path.join(otherpath,filename)  ! G! o4 k% I! R, q1 j9 U, C
          otherfile=open(othername,'wb')
& i# K( U- j: o' ^  X' ?; h; r, Q2 m          for line in open(fullname,'rb'):
' I7 P, m, @$ Z9 B; v              for c in line:
" t$ Z6 c5 C3 i3 K                  if not c.isdigit():otherfile.write(c). s* `2 {* a  }- y! K3 N+ ]
          otherfile.close()

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2019-11-6 17:00:06 | 显示全部楼层
# coding:utf-8: g: P8 a  I6 m' z; q' }2 D- o1 _  x% s
path = r"C:\Users\Administrator\Desktop\CSDN博客草稿\文件的读\password.txt"$ d" K6 b( r) E+ q- e! `& `) U
#传入要读的文件路径; ?$ n5 a) {% _0 K" G
file = open(path,"r",encoding="utf-8",errors="ignore")
# U: ~4 L: H  U4 k9 V"""
( Q" n; c: s- @7 {+ j6 C6 Hopen表示打开你要执行的文件用读的方式打开% s9 ~% c- s+ v
第一个参数是上面的文件path路径,第二个是所要执行的操作,(r)代表读,
% p+ T! a" {% U- U#encoding="utf-8表示指定编码为“utf-8”,errors="ignore"表示读的时候遇到错误忽略
: c+ K* y) v9 O; q"""
8 `( I0 K# @8 lwhile True:/ @6 ]! @* T% n6 Y# l
  mystr = file.readline()#表示一次读取一行
4 i# Z/ J; X9 }! V) A, w  if not mystr:
3 P% ~0 K6 c: @2 I! H  #读到数据最后跳出,结束循环。数据的最后也就是读不到数据了,mystr为空的时候7 M8 \$ O) w0 b1 H( l2 O& F
    break
+ x0 n- C8 z  w2 b+ d  print(mystr,end="")#打印每次读到的内容
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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