Python实现的根据文件名查找数据文件功能示例

yipeiwu_com6年前Python基础

本文实例讲述了Python实现的根据文件名查找数据文件功能。分享给大家供大家参考,具体如下:

#-*- coding: UTF-8 -*-
import os
import shutil
AllFiles=[]
NameFiles=[]
def findFie(filePath):
 pathDir = os.listdir(filePath)
 for allDir in pathDir:
  # print(allDir)
  AllFiles.append(allDir)
  #pass
#filepath = 'C:\\Users\\IBM_ADMIN\\Desktop\\cognos\\datastage\\71&72\\71\\71sns'
#copyfile = 'C:\\Users\\IBM_ADMIN\\Desktop\\cognos\\datastage\\71&72\\71mtp'
filepath = 'C:\\Users\\IBM_ADMIN\\Desktop\\cognos\\datastage\\71&72\\72\\72sns'
copyfile = 'C:\\Users\\IBM_ADMIN\\Desktop\\cognos\\datastage\\71&72\\72mtp'
shutil.rmtree(copyfile)
os.mkdir(copyfile)
findFie(filepath)
def readFile():
 readFile = open('./jobname')
 i = 0
 for eachLine in readFile:
  i= i + 1
  #print(eachLine)
  NameFiles.append(eachLine.replace('\n','')) # 去掉换行符
readFile()
#字符串比较
def doTheCompare():
 for x in NameFiles:
  print(x)
  for y in AllFiles:
   if x == y :
    copyFrom = os.path.join(filepath,x)
    copyTo = os.path.join(copyfile,x)
    shutil.copyfile(copyFrom,copyTo)
   else:
    pass
    #print ("file not find under sns process,thanks .please check with wumi.")
doTheCompare()

附:这里再补充一个更为简单的文件搜索功能示例:

# -*- coding:utf-8 -*-
import os
def search(path=".", name="1"):
  for item in os.listdir(path):
    item_path = os.path.join(path, item)
    if os.path.isdir(item_path):
      search(item_path, name)
    elif os.path.isfile(item_path):
      if name in item:
        print(item_path)
if __name__ == "__main__":
  search(path=r"D:\360Downloads",name="dll")

更多Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

web.py获取上传文件名的正确方法

直接切入主题,从HTML页面上传文件,Python接收处理。但其中发现有些小问题,把它写出来,算是积累吧! HTML页面代码: 复制代码 代码如下: <form action="/...

python3+PyQt5+Qt Designer实现扩展对话框

python3+PyQt5+Qt Designer实现扩展对话框

本文是对《Python Qt GUI快速编程》的第9章的扩展对话框例子Find and replace用Python3+PyQt5+Qt Designer进行改写。 第一部分无借用Qt...

对python中raw_input()和input()的用法详解

最近用到raw_input()和input()来实现即时输入,就顺便找了些资料来看,加上自己所用到的一些内容,整理如下: 1、raw_input() raw_input([promp...

介绍Python中的__future__模块

Python的每个新版本都会增加一些新的功能,或者对原来的功能作一些改动。有些改动是不兼容旧版本的,也就是在当前版本运行正常的代码,到下一个版本运行就可能不正常了。 从Python 2....

python创建只读属性对象的方法(ReadOnlyObject)

复制代码 代码如下:def ReadOnlyObject(**args):    dictBI = {}    args_n...