Python使用for循环依次打开该目录下的各文件

import os
path = r"F:\Python\第一周作业\task"
for filename in os.listdir(path):
print(path,filename)
第一步已经做出来了,出来了文件路径和文件名
(2)、如何使用for循环依次打开该目录下的各文件。
(3)、如何打开文件后,使用for循环依次遍历文档里面的每个字符信息,通过if判断,如果遇到数字,则通过 字符.replace(“”,”待替换的字符”) 替换成空白字符(即删除)。

(4)、如何将改后的文档已同名文件保存到其他目录下。

import os
path = r"F:\Python\第一周作业\task"
otherpath=r"F:\Python\其它目录"
for filename in os.listdir(path):
    print(path,filename)
    fullname=os.path.join(path,filename)
    if os.path.isfile(fullname):        
          othername=os.path.join(otherpath,filename)  
          otherfile=open(othername,'wb')
          for line in open(fullname,'rb'):
              for c in line:
                  if not c.isdigit():otherfile.write(c)
          otherfile.close()

追问

import os
import re
#所在文件路径
file_path = os.listdir('F:/Python/第一周作业/task/')
for files in file_path:

#遍历所有

file = open('F:/Python/第一周作业/task/' + files)
files_name = file.read()
#匹配数字
find=re.findall(r"\d+",files_name)
print(find)
你那个打不开呀,我自己改进了下,就剩下替换数字

温馨提示:答案为网友推荐,仅供参考