简体   繁体   English

使用python删除文件夹和子文件夹中的pdf文件?

[英]Delete pdf files in folders and subfolders with python?

I try to delete 300 pdf files. 我尝试删除300个pdf文件。 All the pdf files as different names, and they all spread in one big folder that divided to a lot of sub folders and sub sub folders. 所有pdf文件都是不同的名称,它们都分布在一个大文件夹中,该文件夹分为很多子文件夹和子子文件夹。 How can I do it with python (I work with python 2.7.8)? 我怎么能用python(我使用python 2.7.8)?

Using shutil.rmtree , you can delete directories recursively. 使用shutil.rmtree ,您可以递归地删除目录。

import shutil
shutil.rmtree('/path/to/directory/that/contains/pdfs')

If the directory contians other files that is not pdf file, use following instead (which use os.walk to traverse the directories recursively, and os.remove / os.unlink to remove the pdf file). 如果目录包含非pdf文件的其他文件,请使用以下代码(使用os.walk以递归方式遍历目录,并使用os.remove / os.unlink删除pdf文件)。

import os

for parent, dirnames, filenames in os.walk('/path/to/the/directory'):
    for fn in filenames:
        if fn.lower().endswith('.pdf'):
            os.remove(os.path.join(parent, fn))

If all you want is delete just the pdf file you can use the os.walk function and fnmatch.fnmatch function. 如果您只想删除pdf文件,则可以使用os.walk函数和fnmatch.fnmatch函数。

import os
from fnmatch import fnmatch

for dirpath, dirnames, filenames in os.walk(os.curdir):
    for file in filenames:
        if fnmatch(file, '*.pdf'):
            os.remove(os.path.join(dirpath, file))

os.chdir to change directory. os.chdir更改目录。 just do some tweak to locate to other directory 只是做一些调整,找到其他目录

 #!/usr/bin/env python
    import glob
    import os
    directory='/path/folder1/folder2'
    os.chdir(directory)
    files=glob.glob('*.pdf')
    for filename in files:
        os.unlink(filename)

Assuming you want to delete the files while maintainig the subfolders tree, you could use a recursive algorithm: 假设您想要在维护子文件夹树的同时删除文件,您可以使用递归算法:

import os

def recursively_remove_files(f):
    if os.path.isfile(f):
        os.unlink(f)
    elif os.path.isdir(f):
        map(recursively_remove_files, [os.path.join(f,fi) for fi in os.listdir(f)])

recursively_remove_files(my_directory)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 遍历文件夹,python中文件的几个子文件夹 - traversing folders, several subfolders for the files in python 使用Python计算所有文件夹/子文件夹中的所有文件 - Count all files in all folders/subfolders with Python Python:如何保存文件夹和子文件夹中的文件? - Python: How to save files from folders and subfolders? 使用python复制文件夹和子文件夹,但只复制子文件夹中的第一个文件 - Copy folders and subfolders, but only first files in subfolders with python 在文件夹和子文件夹中搜索所有Word和PDF文件中的字符串 - Search folders and subfolders for a string within all Word and PDF files Python-使用python ftplib模块下载所有文件夹,子文件夹和文件 - Python - download all folders, subfolders, and files with the python ftplib module 将xlsx文件转换为Excel VBA或Python中的文件夹和子文件夹内的xls - convert xlsx files to xls inside folders and subfolders in Excel VBA or Python Python:如何在ALL文件,文件夹和子文件夹的名称中用下划线替换空格? - Python: How to replace whitespaces by underscore in the name of ALL files, folders and subfolders? 在python中从ftp文件夹和子文件夹读取所有文件 - Reading all files from ftp folders and subfolders in python 使用 Python 搜索(在文件夹和子文件夹中)并将文件读取到数据帧列表 - Search (in folders and subfolders ) and read files to a list of dataframes, using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM