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

yipeiwu_com5年前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程序设计有所帮助。

相关文章

python PIL/cv2/base64相互转换实例

PIL和cv2是python中两个常用的图像处理库,PIL一般是anaconda自带的,cv2是opencv的python版本。base64在网络传输图片的时候经常用到。 ##PIL...

Python3实现计算两个数组的交集算法示例

本文实例讲述了Python3实现计算两个数组的交集算法。分享给大家供大家参考,具体如下: 问题: 给定两个数组,写一个方法来计算它们的交集。 方案一:利用collections.Co...

python操作小程序云数据库实现简单的增删改查功能

python操作小程序云数据库实现简单的增删改查功能

不止python,你可以利用任何语言那实现通过http请求来操作你自己的小程序云数据库了 背景 也是在最近吧,小程序更新了云开发 HTTP API 文档,提供了小程序外访问云开发资源的能...

Python3 入门教程 简单但比较不错

本文适合有Java编程经验的程序员快速熟悉Python 本文程序在windows xp+python3.1a1 测试通过. 本文提到的idle指python shell,即安装pytho...

网站渗透常用Python小脚本查询同ip网站

网站渗透常用Python小脚本查询同ip网站

旁站查询来源: http://dns.aizhan.com http://s.tool.chinaz.com/same http://i.links.cn/sameip/ http://...