程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python file processing -- traversing, matching, and processing files

編輯:Python

Document processing —— Don't Pages!

Realization principle

use os.walk Traverse all file names , There is ‘.pages’ File path , And use os.remove Delete these files

Code

# Character painting comes from this website , Rich font types 
#http://www.network-science.de/ascii/
""" _ _ ____ _____ _____ ______ _____ | \ | | / __ \ | __ \ /\ / ____|| ____| / ____| | \| || | | || |__) |/ \ | | __ | |__ | (___ | . ` || | | || ___// /\ \ | | |_ || __| \___ \ | |\ || |__| || | / ____ \| |__| || |____ ____) | |_| \_| \____/ |_| /_/ \_\\_____||______||_____/ """
import os
file_dir = '/Users' # Start from this path to find the file 
f = [] # Storage file name 
r = [] # Storage file path 
for root, dirs, files in os.walk(file_dir): # Traverse the file names and paths of all files 
for name in files: # Traverse the file name , Yes .pages Just write it down 
if '.pages' in name:
f.append(name)
r.append(root)
print('Found %d sneaky .pages files in your computer'%len(f))
if len(f) != 0:
print('They are: ', f)
# Deleting the comments in the next few lines will directly delete these files 
# i = 0
# for i in range(0, len(f)):
# os.remove(r[i]+'//'+f[i]) # The first slash indicates that the second slash is not a special character 
# print('Successfully deleted '+f[i])

For special reasons , I am extremely disgusted with the apple system pages file , So just write a program to practice .

Functional expansion

The code applies to mac System .windows System handle file_dir Of ’/Users’ Change to C disc D The plates are the same , The later path representation may also need to be adjusted .
use pyinstaller/py2exe Wait for the package to convert the program into .exe file . Change the matching condition of the file name you want to delete , Put it in U pan , Add one .inf file , Can let the computer insert U This program runs automatically when the disk , Delete file .( It is strongly recommended that you do not try ! Those who cause losses should bear their own responsibility !!)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved