Home Backend Development Python Tutorial python实现搜索本地文件信息写入文件的方法

python实现搜索本地文件信息写入文件的方法

Jun 10, 2016 pm 03:06 PM
python write file search

本文实例讲述了python实现搜索本地文件信息写入文件的方法。分享给大家供大家参考,具体如下:

主要功能:

在指定的盘符,如D盘,搜索出与用户给定后缀名(如:jpg,png)相关的文件,然后把搜索出来的信息(相关文件的绝对路径),存放到用户指定的文件(如果文件不存在,则建立相应的文件)中

先卡看运行效果吧:

运行效果的前部分:

运行效果的后部分:

写入信息后的文件:

代码部分:

#在指定的盘符,如D盘,搜索出与用户给定后缀名(如:jpg,png)相关的文件
#然后把搜索出来的信息(相关文件的绝对路径),存放到用户指定的
#文件(如果文件不存在,则建立相应的文件)中
import os
import time
#指定盘符
DESK = 'E:\\'
#信息保存文件的路径
##########    这里请先建立好此文件,我在做文件操作的过程中
##########    使用os.mknod('E:\\info.txt'),系统不会建立文件的
SAVE_FILE = 'E:\\info.txt'
#文件后缀类型
FILE_EXT = ['bmp','jpeg','gif','psd','png','jpg']
#定义全局变量
my_dirs = []
my_files = []
#文件个数
FILES_NUMBER = 0
#符合要求的文件个数
RIGHT_FILES_NUMBER = 0
#不符合要求的文件个数
NOT_RIGHT_FILES_NUMBER = 0
#文件夹个数
DIR_NUMBER = 0
#获取指定文件夹下面的所有文件及文件夹
#如果指定的文件夹不存在,则返回相应的提示信息
def listdir(dir_path):
  if os.path.exists(dir_path):
    return os.listdir(dir_path)
  else:
    return '目录'+ dir_path + '不存在'
#搜索文件主函数
def search_files(path,name):
  if not os.path.isdir(path) and not os.path.isfile(path):
    return False
  path = os.path.join(path,name)
  if os.path.isfile(path): #是文件
    global FILES_NUMBER
    FILES_NUMBER = FILES_NUMBER + 1
    lists = path.split('.')
    #print('============================================',lists)
    file_ext = lists[-1] #文件扩展名
    if file_ext in FILE_EXT:
      global RIGHT_FILES_NUMBER
      RIGHT_FILES_NUMBER = RIGHT_FILES_NUMBER + 1
      global my_files
      now = str(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
      size = str(get_file_size(path))
      my_files.append(now+'  '+path+'  '+size+'\n')
      print('文件:',path)
    else:
      global NOT_RIGHT_FILES_NUMBER
      NOT_RIGHT_FILES_NUMBER = NOT_RIGHT_FILES_NUMBER + 1
  elif os.path.isdir(path): #是文件夹
    global DIR_NUMBER
    DIR_NUMBER = DIR_NUMBER + 1
    for name in listdir(path):
      #print(os.path.join(path,name))
      search_files(path,name)
#获取文件大小
def get_file_size(path):
  if os.path.exists(path):
    return os.path.getsize(path)
#写入信息
def write_info(content):
  if os.path.exists(path):
    with open(SAVE_FILE,'w+') as fp:
      fp.write(content)
      fp.flush()
      fp.close()
  else:
    print('文件:{}不存在!'.format(SAVE_FILE))
#读取所有信息
def read_info():
  if os.path.exists(path):
    with open(SAVE_FILE,'r+') as fp:
      for line in fp:
        print(line)
  else:
    print('文件:{}不存在!'.format(SAVE_FILE))
if __name__ == '__main__':
  for d in listdir(DESK):
    my_dirs.append(os.path.join(DESK,d))
  print(my_dirs)
  #这里是做测试用的,由于扫描整个盘符涉及到的文件和文件夹很多,可能要花一定的时间
  #所以这里可以使用一个文件夹作为测试
  my_dir = ['E:\\test']
  for path in my_dir:
    search_files(path,'')
  print('#' * 50)
  print(my_files)
  print('#' * 50)
  print('开始写入信息...')
  content = ''.join(my_files)
  write_info(content)
  print('#' * 50)
  print('开始读取信息...')
  read_info()
  print('#' * 50)
  print('搜索文件夹总数:{0},文件总数:{1}'.format(DIR_NUMBER,FILES_NUMBER))
  print('符合要求的文件总数:{0},不符合要求的文件总数:{1}'.format(RIGHT_FILES_NUMBER,NOT_RIGHT_FILES_NUMBER))

Copy after login

运行控制台情况;

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
['E:\\bb', 'E:\\devlopment', 'E:\\game', 'E:\\hongten_download', 'E:\\info.txt', 'E:\\log4j', 'E:\\mydir', 'E:\\oracle', 'E:\\oracle10.2_win32', 'E:\\RECYCLER', 'E:\\svn_checkout', 'E:\\System Volume Information', 'E:\\test', 'E:\\The KMPlayer', 'E:\\windows', 'E:\\work']
文件: E:\test\20130627_140132Hongten.jpg
文件: E:\test\20130627_182913(1)Hongten.jpg
文件: E:\test\20130627_183008(1)Hongten.jpg
文件: E:\test\20130627_183054Hongten.jpg
文件: E:\test\20130627_183059Hongten.jpg
文件: E:\test\20130627_183101Hongten.jpg
文件: E:\test\20130627_183116Hongten.jpg
文件: E:\test\20130627_183326Hongten.jpg
文件: E:\test\20130627_183714Hongten.jpg
文件: E:\test\20130627_183749Hongten.jpg
文件: E:\test\20130627_183925Hongten.jpg
文件: E:\test\20130627_203658Hongten.jpg
文件: E:\test\20130627_203802Hongten.jpg
文件: E:\test\20130627_205112(1)Hongten.jpg
文件: E:\test\20130627_205131Hongten.jpg
文件: E:\test\20130627_205159Hongten.jpg
文件: E:\test\20130627_205219(1)Hongten.jpg
文件: E:\test\20130627_205257Hongten.jpg
文件: E:\test\20130627_205315Hongten.jpg
文件: E:\test\20130627_205408(1)Hongten.jpg
文件: E:\test\20130627_205425Hongten.jpg
文件: E:\test\20130627_205627Hongten.jpg
文件: E:\test\20130627_205629Hongten.jpg
文件: E:\test\hongten\6.27\20130627_140132Hongten.jpg
文件: E:\test\hongten\6.27\20130627_182913(1)Hongten.jpg
文件: E:\test\hongten\6.27\20130627_183008(1)Hongten.jpg
文件: E:\test\hongten\6.27\20130627_183054Hongten.jpg
文件: E:\test\hongten\6.27\20130627_183059Hongten.jpg
文件: E:\test\hongten\6.27\20130627_183101Hongten.jpg
文件: E:\test\hongten\6.27\20130627_183116Hongten.jpg
文件: E:\test\hongten\6.27\20130627_183326Hongten.jpg
文件: E:\test\hongten\6.27\20130627_183714Hongten.jpg
文件: E:\test\hongten\6.27\20130627_183749Hongten.jpg
文件: E:\test\hongten\6.27\20130627_183925Hongten.jpg
文件: E:\test\hongten\6.27\20130627_203658Hongten.jpg
文件: E:\test\hongten\6.27\20130627_203802Hongten.jpg
文件: E:\test\hongten\6.27\20130627_205112(1)Hongten.jpg
文件: E:\test\hongten\6.27\20130627_205131Hongten.jpg
文件: E:\test\hongten\6.27\20130627_205159Hongten.jpg
文件: E:\test\hongten\6.27\20130627_205219(1)Hongten.jpg
文件: E:\test\hongten\6.27\20130627_205257Hongten.jpg
文件: E:\test\hongten\6.27\20130627_205315Hongten.jpg
文件: E:\test\hongten\6.27\20130627_205408(1)Hongten.jpg
文件: E:\test\hongten\6.27\20130627_205425Hongten.jpg
文件: E:\test\hongten\6.27\20130627_205627Hongten.jpg
文件: E:\test\hongten\6.27\20130627_205629Hongten.jpg
##################################################
['2013-07-28 17:14:49  E:\\test\\20130627_140132Hongten.jpg  0\n', '2013-07-28 17:14:49  E:\\test\\20130627_182913(1)Hongten.jpg  2380747\n', '2013-07-28 17:14:49  E:\\test\\20130627_183008(1)Hongten.jpg  2315326\n', '2013-07-28 17:14:49  E:\\test\\20130627_183054Hongten.jpg  2672977\n', '2013-07-28 17:14:49  E:\\test\\20130627_183059Hongten.jpg  2006608\n', '2013-07-28 17:14:49  E:\\test\\20130627_183101Hongten.jpg  2076974\n', '2013-07-28 17:14:49  E:\\test\\20130627_183116Hongten.jpg  2687018\n', '2013-07-28 17:14:49  E:\\test\\20130627_183326Hongten.jpg  1993448\n', '2013-07-28 17:14:49  E:\\test\\20130627_183714Hongten.jpg  2497973\n', '2013-07-28 17:14:49  E:\\test\\20130627_183749Hongten.jpg  2066327\n', '2013-07-28 17:14:49  E:\\test\\20130627_183925Hongten.jpg  2037776\n', '2013-07-28 17:14:49  E:\\test\\20130627_203658Hongten.jpg  3033996\n', '2013-07-28 17:14:49  E:\\test\\20130627_203802Hongten.jpg  2837396\n', '2013-07-28 17:14:49  E:\\test\\20130627_205112(1)Hongten.jpg  2987659\n', '2013-07-28 17:14:49  E:\\test\\20130627_205131Hongten.jpg  2745724\n', '2013-07-28 17:14:49  E:\\test\\20130627_205159Hongten.jpg  2824810\n', '2013-07-28 17:14:49  E:\\test\\20130627_205219(1)Hongten.jpg  2864744\n', '2013-07-28 17:14:49  E:\\test\\20130627_205257Hongten.jpg  3092656\n', '2013-07-28 17:14:49  E:\\test\\20130627_205315Hongten.jpg  2832393\n', '2013-07-28 17:14:49  E:\\test\\20130627_205408(1)Hongten.jpg  2796261\n', '2013-07-28 17:14:49  E:\\test\\20130627_205425Hongten.jpg  3295286\n', '2013-07-28 17:14:49  E:\\test\\20130627_205627Hongten.jpg  2819717\n', '2013-07-28 17:14:49  E:\\test\\20130627_205629Hongten.jpg  2813522\n', '2013-07-28 17:14:49  E:\\test\\hongten\\6.27\\20130627_140132Hongten.jpg  2571032\n', '2013-07-28 17:14:49  E:\\test\\hongten\\6.27\\20130627_182913(1)Hongten.jpg  2380747\n', '2013-07-28 17:14:49  E:\\test\\hongten\\6.27\\20130627_183008(1)Hongten.jpg  2315326\n', '2013-07-28 17:14:49  E:\\test\\hongten\\6.27\\20130627_183054Hongten.jpg  2672977\n', '2013-07-28 17:14:49  E:\\test\\hongten\\6.27\\20130627_183059Hongten.jpg  2006608\n', '2013-07-28 17:14:49  E:\\test\\hongten\\6.27\\20130627_183101Hongten.jpg  2076974\n', '2013-07-28 17:14:49  E:\\test\\hongten\\6.27\\20130627_183116Hongten.jpg  2687018\n', '2013-07-28 17:14:49  E:\\test\\hongten\\6.27\\20130627_183326Hongten.jpg  1993448\n', '2013-07-28 17:14:49  E:\\test\\hongten\\6.27\\20130627_183714Hongten.jpg  2497973\n', '2013-07-28 17:14:49  E:\\test\\hongten\\6.27\\20130627_183749Hongten.jpg  2066327\n', '2013-07-28 17:14:50  E:\\test\\hongten\\6.27\\20130627_183925Hongten.jpg  2037776\n', '2013-07-28 17:14:50  E:\\test\\hongten\\6.27\\20130627_203658Hongten.jpg  3033996\n', '2013-07-28 17:14:50  E:\\test\\hongten\\6.27\\20130627_203802Hongten.jpg  2837396\n', '2013-07-28 17:14:50  E:\\test\\hongten\\6.27\\20130627_205112(1)Hongten.jpg  2987659\n', '2013-07-28 17:14:50  E:\\test\\hongten\\6.27\\20130627_205131Hongten.jpg  2745724\n', '2013-07-28 17:14:50  E:\\test\\hongten\\6.27\\20130627_205159Hongten.jpg  2824810\n', '2013-07-28 17:14:50  E:\\test\\hongten\\6.27\\20130627_205219(1)Hongten.jpg  2864744\n', '2013-07-28 17:14:50  E:\\test\\hongten\\6.27\\20130627_205257Hongten.jpg  3092656\n', '2013-07-28 17:14:50  E:\\test\\hongten\\6.27\\20130627_205315Hongten.jpg  2832393\n', '2013-07-28 17:14:50  E:\\test\\hongten\\6.27\\20130627_205408(1)Hongten.jpg  2796261\n', '2013-07-28 17:14:50  E:\\test\\hongten\\6.27\\20130627_205425Hongten.jpg  3295286\n', '2013-07-28 17:14:50  E:\\test\\hongten\\6.27\\20130627_205627Hongten.jpg  2819717\n', '2013-07-28 17:14:50  E:\\test\\hongten\\6.27\\20130627_205629Hongten.jpg  2813522\n']
##################################################
开始写入信息...
##################################################
开始读取信息...
2013-07-28 17:14:49  E:\test\20130627_140132Hongten.jpg  0
2013-07-28 17:14:49  E:\test\20130627_182913(1)Hongten.jpg  2380747
2013-07-28 17:14:49  E:\test\20130627_183008(1)Hongten.jpg  2315326
2013-07-28 17:14:49  E:\test\20130627_183054Hongten.jpg  2672977
2013-07-28 17:14:49  E:\test\20130627_183059Hongten.jpg  2006608
2013-07-28 17:14:49  E:\test\20130627_183101Hongten.jpg  2076974
2013-07-28 17:14:49  E:\test\20130627_183116Hongten.jpg  2687018
2013-07-28 17:14:49  E:\test\20130627_183326Hongten.jpg  1993448
2013-07-28 17:14:49  E:\test\20130627_183714Hongten.jpg  2497973
2013-07-28 17:14:49  E:\test\20130627_183749Hongten.jpg  2066327
2013-07-28 17:14:49  E:\test\20130627_183925Hongten.jpg  2037776
2013-07-28 17:14:49  E:\test\20130627_203658Hongten.jpg  3033996
2013-07-28 17:14:49  E:\test\20130627_203802Hongten.jpg  2837396
2013-07-28 17:14:49  E:\test\20130627_205112(1)Hongten.jpg  2987659
2013-07-28 17:14:49  E:\test\20130627_205131Hongten.jpg  2745724
2013-07-28 17:14:49  E:\test\20130627_205159Hongten.jpg  2824810
2013-07-28 17:14:49  E:\test\20130627_205219(1)Hongten.jpg  2864744
2013-07-28 17:14:49  E:\test\20130627_205257Hongten.jpg  3092656
2013-07-28 17:14:49  E:\test\20130627_205315Hongten.jpg  2832393
2013-07-28 17:14:49  E:\test\20130627_205408(1)Hongten.jpg  2796261
2013-07-28 17:14:49  E:\test\20130627_205425Hongten.jpg  3295286
2013-07-28 17:14:49  E:\test\20130627_205627Hongten.jpg  2819717
2013-07-28 17:14:49  E:\test\20130627_205629Hongten.jpg  2813522
2013-07-28 17:14:49  E:\test\hongten\6.27\20130627_140132Hongten.jpg  2571032
2013-07-28 17:14:49  E:\test\hongten\6.27\20130627_182913(1)Hongten.jpg  2380747
2013-07-28 17:14:49  E:\test\hongten\6.27\20130627_183008(1)Hongten.jpg  2315326
2013-07-28 17:14:49  E:\test\hongten\6.27\20130627_183054Hongten.jpg  2672977
2013-07-28 17:14:49  E:\test\hongten\6.27\20130627_183059Hongten.jpg  2006608
2013-07-28 17:14:49  E:\test\hongten\6.27\20130627_183101Hongten.jpg  2076974
2013-07-28 17:14:49  E:\test\hongten\6.27\20130627_183116Hongten.jpg  2687018
2013-07-28 17:14:49  E:\test\hongten\6.27\20130627_183326Hongten.jpg  1993448
2013-07-28 17:14:49  E:\test\hongten\6.27\20130627_183714Hongten.jpg  2497973
2013-07-28 17:14:49  E:\test\hongten\6.27\20130627_183749Hongten.jpg  2066327
2013-07-28 17:14:50  E:\test\hongten\6.27\20130627_183925Hongten.jpg  2037776
2013-07-28 17:14:50  E:\test\hongten\6.27\20130627_203658Hongten.jpg  3033996
2013-07-28 17:14:50  E:\test\hongten\6.27\20130627_203802Hongten.jpg  2837396
2013-07-28 17:14:50  E:\test\hongten\6.27\20130627_205112(1)Hongten.jpg  2987659
2013-07-28 17:14:50  E:\test\hongten\6.27\20130627_205131Hongten.jpg  2745724
2013-07-28 17:14:50  E:\test\hongten\6.27\20130627_205159Hongten.jpg  2824810
2013-07-28 17:14:50  E:\test\hongten\6.27\20130627_205219(1)Hongten.jpg  2864744
2013-07-28 17:14:50  E:\test\hongten\6.27\20130627_205257Hongten.jpg  3092656
2013-07-28 17:14:50  E:\test\hongten\6.27\20130627_205315Hongten.jpg  2832393
2013-07-28 17:14:50  E:\test\hongten\6.27\20130627_205408(1)Hongten.jpg  2796261
2013-07-28 17:14:50  E:\test\hongten\6.27\20130627_205425Hongten.jpg  3295286
2013-07-28 17:14:50  E:\test\hongten\6.27\20130627_205627Hongten.jpg  2819717
2013-07-28 17:14:50  E:\test\hongten\6.27\20130627_205629Hongten.jpg  2813522
##################################################
搜索文件夹总数:4,文件总数:50
符合要求的文件总数:46,不符合要求的文件总数:4
>>> 

Copy after login

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

See all articles