> ## Content Index
> Fetch the complete content index at: https://lucent.blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# Python查找文件并压缩
- URL: https://lucent.blog/pythoncha-zhao-wen-jian-bing-ya-suo/
- Published: 2020-06-12T04:20:42.000Z
- Updated: 2026-08-21T00:49:55.000Z
- Author: Lucent
- Tags: Python

代码:

```import
my = os.path.abspath(r'C:\\Users\\user\\Desktop\\新建文件夹\\111')
cp = os.path.abspath(r'D:\\PicFile')

if not os.path.exists(cp):
    os.makedirs(cp)
t = 0
q = 0
a = input('请输入需要查找到的文件扩展名：')

if os.path.exists(my):
    for root, dirs, files in os.walk(my):
        for file in files :
            src_file = os.path.join(root, file)
            if a in src_file:
                q += 1
print('\n'
      '==============共找到%d文件==============='
      '\n' % (q))
if os.path.exists(my):
    for root, dirs, files in os.walk(my):
        for file in files :
            src_file = os.path.join(root, file)
            if a in src_file:
                t = t + 1
                print(t,src_file)
                shutil.copy(src_file, cp)

shutil.make_archive('all_python','zip',base_dir='D:\\myFile')

```