python实现搜索本地文件信息写入文件的方法
本文实例讲述了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))
运行控制台情况;
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 >>>
希望本文所述对大家Python程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

An application that converts XML directly to PDF cannot be found because they are two fundamentally different formats. XML is used to store data, while PDF is used to display documents. To complete the transformation, you can use programming languages and libraries such as Python and ReportLab to parse XML data and generate PDF documents.

There is no APP that can convert all XML files into PDFs because the XML structure is flexible and diverse. The core of XML to PDF is to convert the data structure into a page layout, which requires parsing XML and generating PDF. Common methods include parsing XML using Python libraries such as ElementTree and generating PDFs using ReportLab library. For complex XML, it may be necessary to use XSLT transformation structures. When optimizing performance, consider using multithreaded or multiprocesses and select the appropriate library.

It is impossible to complete XML to PDF conversion directly on your phone with a single application. It is necessary to use cloud services, which can be achieved through two steps: 1. Convert XML to PDF in the cloud, 2. Access or download the converted PDF file on the mobile phone.

XML beautification is essentially improving its readability, including reasonable indentation, line breaks and tag organization. The principle is to traverse the XML tree, add indentation according to the level, and handle empty tags and tags containing text. Python's xml.etree.ElementTree library provides a convenient pretty_xml() function that can implement the above beautification process.

The speed of mobile XML to PDF depends on the following factors: the complexity of XML structure. Mobile hardware configuration conversion method (library, algorithm) code quality optimization methods (select efficient libraries, optimize algorithms, cache data, and utilize multi-threading). Overall, there is no absolute answer and it needs to be optimized according to the specific situation.

To generate images through XML, you need to use graph libraries (such as Pillow and JFreeChart) as bridges to generate images based on metadata (size, color) in XML. The key to controlling the size of the image is to adjust the values of the <width> and <height> tags in XML. However, in practical applications, the complexity of XML structure, the fineness of graph drawing, the speed of image generation and memory consumption, and the selection of image formats all have an impact on the generated image size. Therefore, it is necessary to have a deep understanding of XML structure, proficient in the graphics library, and consider factors such as optimization algorithms and image format selection.

Use most text editors to open XML files; if you need a more intuitive tree display, you can use an XML editor, such as Oxygen XML Editor or XMLSpy; if you process XML data in a program, you need to use a programming language (such as Python) and XML libraries (such as xml.etree.ElementTree) to parse.

There is no simple and direct free XML to PDF tool on mobile. The required data visualization process involves complex data understanding and rendering, and most of the so-called "free" tools on the market have poor experience. It is recommended to use computer-side tools or use cloud services, or develop apps yourself to obtain more reliable conversion effects.
